刚详细看了下原来是比较器写错了
return sge = num == 0 ? 1 : num; 这个我是真没看懂
我这么写是可以的
TreeMap<Person, String> tm = new TreeMap<>(new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
int num = p1.getAge()-p2.getAge();
return num ==0 ?p1.getName().compareTo(p2.getName()):num ;
}
});
tm.put(new Person("zhangsan",23),"就业班");
tm.put(new Person("lisi",24), "基础班");
tm.put(new Person("wangwu",23), "就业班");
tm.put(new Person("zhaoliu",25), "就业班");
tm.put(new Person("zhouqi",26), "基础班");
tm.put(new Person("qianba",25), "就业班");
tm.put(new Person("qianba",25), "就业班");
System.out.println(tm);
for (Person p : tm.keySet()) {
System.out.println(p+ "---"+ tm.get(p));
}
控制台输出
{Person [name=wangwu, age=23]=就业班, Person [name=zhangsan, age=23]=就业班, Person [name=lisi, age=24]=基础班, Person [name=qianba, age=25]=就业班, Person [name=zhaoliu, age=25]=就业班, Person [name=zhouqi, age=26]=基础班}
Person [name=wangwu, age=23]---就业班
Person [name=zhangsan, age=23]---就业班
Person [name=lisi, age=24]---基础班
Person [name=qianba, age=25]---就业班
Person [name=zhaoliu, age=25]---就业班
Person [name=zhouqi, age=26]---基础班
|