public static void main(String[] args) {
TreeMap<Student,String> tm = new TreeMap<>(new Comparator<Student>(){
public int compare(Student s1,Student s2){
int num = s1.getAge() - s2.getAge();
return num == 0 ? 1 :num;
}
});
tm.put(new Student("王五",25), "上海");
tm.put(new Student("张三",23), "北京");
tm.put(new Student("李四",24), "深圳");
System.out.println(tm);
for(Student s : tm.keySet()) {
System.out.println(s + " ..." + tm.get(s));
}
}
输出:Student [name=张三, age=23] ...null
Student [name=李四, age=24] ...null
Student [name=王五, age=25] ...null |
|