public static void main(String[] args) {
/*
* :案例演示 TreeMap集合键是Student值是String的案例
*/
TreeMap<Student, String> hm = new TreeMap<>(new Comparator<Student>() {
@Override//传一个比较器
public int compare(Student s1, Student s2) {
int num =s1.getName().compareTo(s2.getName());
return num==0? s1.getAge()-s2.getAge():num;
}
});
//demo1(hm);
hm.put(new Student("张三", 23), "上海");
hm.put(new Student("张三", 24), "北京");
hm.put(new Student("王五", 22), "南京");
hm.put(new Student("赵六", 26), "无锡");
System.out.println(hm);
}
public static void demo1(TreeMap<Student, String> hm) {
hm.put(new Student("张三", 23), "上海");
hm.put(new Student("张三", 23), "北京");
hm.put(new Student("王五", 23), "南京");
hm.put(new Student("赵六", 23), "无锡");
System.out.println(hm);
}
/*
* :案例演示 TreeMap集合键是Student值是String的案例
*/
TreeMap<Student, String> hm = new TreeMap<>(new Comparator<Student>() {
@Override//传一个比较器
public int compare(Student s1, Student s2) {
int num =s1.getName().compareTo(s2.getName());
return num==0? s1.getAge()-s2.getAge():num;
}
});
//demo1(hm);
hm.put(new Student("张三", 23), "上海");
hm.put(new Student("张三", 24), "北京");
hm.put(new Student("王五", 22), "南京");
hm.put(new Student("赵六", 26), "无锡");
System.out.println(hm);
}
public static void demo1(TreeMap<Student, String> hm) {
hm.put(new Student("张三", 23), "上海");
hm.put(new Student("张三", 23), "北京");
hm.put(new Student("王五", 23), "南京");
hm.put(new Student("赵六", 23), "无锡");
System.out.println(hm);
}