类重写接口时要加上泛型,Student
Set<Apple> set = new TreeSet<>(((o1, o2) -> o1.getPrice() - o2.getPrice() >=0 ? 1:-1));
最后三张截取成集合,可以使用addAll()方法合成一个list。
这里的三个人老师直接用三个list表示了,同样不能在card类中重写排序接口的方法。
可以在Test类中定义一个方法对每个人的集合进行排序,使用Collections工具类的sort方法。
怎样实现排序?
[♥10, ♠10, ♥2, ♣2, ♣4, ♦5, ♣5, ♥6, ♦7, ♥7, ♠8, ♥9, ♥A, ♠A, ♦A, ♣A, ♠J, ♠Q]
在Card添加大小属性,其实就是C语言时候的结构体。
Map
自动补全变量名
先用一个map存,然后再用一个map存各个景点出现的次数
public class Test1 {
public final static String[] ATTRACTIONS = {"A","B","C","D"};
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<>();
Random rd = new Random();
for (int i = 0; i < 80; i++) {
map.put(i,ATTRACTIONS[rd.nextInt(4)]);
}
System.out.println(map);
Map<String,Integer> cnt = new HashMap<>();
System.out.println(cnt);
for (String s : map.values()) {
if(cnt.containsKey(s)){
cnt.put(s,cnt.get(s)+1);
}else {
cnt.put(s,1);
}
}
System.out.println(cnt);
}
}
老师直接用字符串表示每个人的选择,用StringBuild连接随机字符。 然后判断每个字符(Character)出现次数。
但是遇到一样的哈希值通过对比内容也一样的话,新的还是会覆盖旧内容
老师直接手动添加的,我随机一个选的个数,然后再随机几个景点,可能会重复,所以我用的set,没用list。
public class Test2 {
public final static String[] ATTRACTIONS = {"A","B","C","D"};
public static void main(String[] args) {
Map<Integer,Set<String>> map = new HashMap<>();
Random rd = new Random();
for (int i = 0; i < 80; i++) {
Set<String> set = new HashSet<>();
for (int j = 0; j < rd.nextInt(4)+1; j++) {
set.add(ATTRACTIONS[rd.nextInt(4)]);
}
map.put(i,set);
}
System.out.println(map);
Map<String,Integer> cnt = new HashMap<>();
System.out.println(cnt);
for (Set<String> stringSet : map.values()) {
for (String s : stringSet) {
if(cnt.containsKey(s)){
cnt.put(s,cnt.get(s)+1);
}else {
cnt.put(s,1);
}
}
}
System.out.println(cnt);
}
}