Map中的键唯一,但是当存储自定义对象时,需要重写Hashcode和equals方法
public class Test4 {
public static void main(String[] args) {
//A:
//案例演示:
//模拟斗地主洗牌和发牌,牌没有排序
//得有一副牌
//生成54张牌放到牌盒里面
String[] colors = {"♠", "♥", "♦", "♣"};
String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
HashMap<Integer, String> map = new HashMap<Integer, String>();
ArrayList<Integer> list = new ArrayList<>();
int index = 0;
for (String num : nums) {
for (String color : colors) {
map.put(index, color.concat(num));
list.add(index);
index++;
}
}
map.put(index, "☆");
list.add(index);
index++;
map.put(index, "★");
list.add(index);
//洗牌
Collections.shuffle(list);
//发牌
TreeSet<Integer> 高进 = new TreeSet<>();
TreeSet<Integer> 刀仔 = new TreeSet<>();
TreeSet<Integer> 星仔 = new TreeSet<>();
TreeSet<Integer> 底牌 = new TreeSet<>();
// 高进 = (ArrayList<String>) pokerBox.subList(0,17);
// 高进 0 3 6 9
//刀仔 1 4 7 10
// 星仔 2 5 8 11
for (int i = 0; i < list.size(); i++) {
if (i >= list.size() - 3) {
底牌.add(list.get(i));
} else if (i % 3 == 0) {
高进.add(list.get(i));
} else if (i % 3 == 1) {
刀仔.add(list.get(i));
} else {
星仔.add(list.get(i));
}
}
//看牌
lookPoker(map,高进,"高进");
lookPoker(map,刀仔,"刀仔");
lookPoker(map,星仔,"星仔");
lookPoker(map,底牌,"底牌");
}
public static void lookPoker(HashMap<Integer, String> map, TreeSet<Integer> list, String name) {
System.out.println(name);
for (Integer s : list) {
System.out.print(map.get(s));
}
System.out.println();
}
}