- 说明
HashMap集合存储的元素的键值是无序的和不可重复的,为了对集合中的元素的键值进行排序,Map接口还有了另一个可以对集合中元素键和值进行排序的实现类TreeMap。
- 测试类
import entity.Student;
import entity.Teacher;
import java.util.Comparator;
import java.util.TreeMap;
/**
* TreeMap 测试
* @author : ZhouMei
* @date Date : 2022年08月11日 14:03
* @Description: TODO
*/
public class TreeMapTest {
public static void main(String[] args) {
studentTest();
teacherTest();
}
/**
* TreeMap 测试 Student 类
* Student实现了Comparable接口,并且重写了compareTo方法。此时TreeMap可以不用自定义比较器Comparator,程序默认使用Student类的compareTo方法
* 当然也可以使用自定义比较器自定义排序规则
*/
private static void studentTest(){
//1、使用Student类的compareTo方法
TreeMap map1 = new TreeMap();
map1.put(new Student(1, "张三", 23), "张三");
map1.put(new Student(2, "李四", 18), "李四");
map1.put(new Student(3, "王五", 22), "王五");
map1.put(new Student(4, "赵六", 21), "赵六");
System.out.println("TreeMap测试Student类,使用Student默认比较器排序后的结果:" + map1);
//2、使用自定义比较器,按年龄倒序
TreeMap map2 = new TreeMap(new Comparator() {
public int compare(Student s1, Student s2) {
//按年龄排序
int num = s2.getAge()-s1.getAge();
//如果年龄相同,再按名字排序
return num!=0? num : s2.getName().compareTo(s1.getName());
}
});
map2.put(new Student(1, "张三", 23), "张三");
map2.put(new Student(2, "李四", 18), "李四");
map2.put(new Student(3, "王五", 22), "王五");
map2.put(new Student(4, "赵六", 21), "赵六");
System.out.println("TreeMap测试Student类,使用自定义比较器排序后的结果:" + map2);
}
/**
* TreeMap 测试 Teacher 类
* Teacher类没有实现Comparable接口,此时TreeMap必须使用自定义比较器Comparator
*/
private static void teacherTest(){
TreeMap map = new TreeMap(new Comparator() {
public int compare(Teacher t1, Teacher t2) {
//按年龄排序
int num = t1.getAge()-t2.getAge();
//如果年龄相同,再按名字排序
return num!=0? num : t1.getName().compareTo(t2.getName());
}
});
map.put(new Teacher(1, "张三", 23), "张三");
map.put(new Teacher(2, "李四", 18), "李四");
map.put(new Teacher(3, "王五", 22), "王五");
map.put(new Teacher(4, "赵六", 21), "赵六");
System.out.println("TreeMap测试Teacher类,使用自定义比较器排序后的结果:" + map);
}
}
package entity;
import lombok.Data;
/**
* @author : ZhouMei
* @date Date : 2022年08月11日 10:20
* @Description: TODO
*/
@Data
public class Student implements Comparable {
private Integer id;
private String name;
private Integer age;
public int compareTo(Object o1) {
Student s1 = (Student) o1;
if(this.age > s1.age){
return 1;
}
if(this.age == s1.age){
return this.name.compareTo(s1.name);
}
return -1;
}
public Student(Integer id, String name, Integer age){
this.id = id;
this.name = name;
this.age = age;
}
}
package entity;
import lombok.Data;
/**
* @author : ZhouMei
* @date Date : 2022年08月11日 14:18
* @Description: TODO
*/
@Data
public class Teacher {
private Integer id;
private String name;
private Integer age;
public Teacher(Integer id, String name, Integer age){
this.id = id;
this.name = name;
this.age = age;
}
}
TreeMap测试Student类,使用Student默认比较器排序后的结果:{Student(id=2, name=李四, age=18)=李四, Student(id=4, name=赵六, age=21)=赵六, Student(id=3, name=王五, age=22)=王五, Student(id=1, name=张三, age=23)=张三}
TreeMap测试Student类,使用自定义比较器排序后的结果:{Student(id=1, name=张三, age=23)=张三, Student(id=3, name=王五, age=22)=王五, Student(id=4, name=赵六, age=21)=赵六, Student(id=2, name=李四, age=18)=李四}
TreeMap测试Teacher类,使用自定义比较器排序后的结果:{Teacher(id=2, name=李四, age=18)=李四, Teacher(id=4, name=赵六, age=21)=赵六, Teacher(id=3, name=王五, age=22)=王五, Teacher(id=1, name=张三, age=23)=张三}