引入
紧接上一版的 Java比较器的使用一(数组版) 这次咱们利用比较器对集合中的对象进行排序。
对象
这次我们使用一个教师对象,有教师的String类型 id,Stirng类型名字,浮点型 工资,我们创建对象并实现内部比较器,按照年龄升序,工资升序的顺序排序。
/**
* @Description:教师类
* @author lzp
* @Date 2020年7月21日
*/
public class Teacher implements Comparable<Teacher> {
private String id;
private String name;
private int age;
private double salary;
public Teacher() {
}
public Teacher(String id, String name, int age, double salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
//比较方法
@Override
public int compareTo(Teacher other) {
int flag = 0;
flag = this.age - other.age;
if(flag == 0) {
if (this.salary - other.salary > 0) {
flag = 1;
} else if (this.salary - other.salary < 0) {
flag = -1;
}
}
return flag;
}
}
创建外部比较器
创建外部比较器TeacherComparator实现Comparator接口,重写比较方法:
import java.util.Comparator;
public class TeacherComparator implements Comparator<Teacher>{
@Override
public int compare(Teacher o1, Teacher o2) {
int flag = 0;
flag = o1.getAge() - o2.getAge();
if(flag == 0) {
if(o1.getSalary() - o2.getSalary()>0) {
flag = 1;
}else if(o1.getSalary() - o2.getSalary()<0) {
flag = -1;
}else {
flag = 0;
}
}
return flag;
}
}
List集合排序
内部比较器
List集合中已经封装了一个sort方法,需要传入比较器,用null作为参数就是使用内部比较器。
ArrayList<Teacher> tchList = new ArrayList<Teacher>();
tchList.add(new Teacher("1001","小王",25,28020));
tchList.add(new Teacher("1002","小李",20,19555));
tchList.add(new Teacher("1003","小龙",25,15666));
tchList.add(new Teacher("1004","老黄",29,23552));
tchList.add(new Teacher("1005","老王",43,37555));
//使用内部比较器
tchList.sort(null);
for (Teacher teacher : tchList) {
System.out.print(teacher+" ");
}
外部比较器
只需要将参数从null改为自定义的比较器即可调用外部比较器。
tchList.sort(new TeacherComparator());
Set集合排序
内部比较器
Set集合中的TreeSet是有序的,且必须实现比较器中的compareTo方法,不然编译就会报错,默认调用的就是内部比较器。
Set<Teacher> tchSet = new TreeSet<Teacher>();
tchSet.add(new Teacher("1001","小王",22,28020));
tchSet.add(new Teacher("1002","小李",20,19555));
tchSet.add(new Teacher("1003","小龙",25,15666));
tchSet.add(new Teacher("1004","老黄",29,23552));
tchSet.add(new Teacher("1005","老王",43,37555));
for (Teacher teacher : tchList) {
System.out.print(teacher+" ");
}
外部比较器
TreeSet集合使用外部比较器只需要将比较器作为参数传入TreeSet的构造函数即可。
Set<Teacher> tchSet = new TreeSet<Teacher>(new TeacherComparator());
Map集合
外部比较器
map集合存储的是键值对,使用treeMap默认只能对键进行排序,如果我们需要通过对象中的属性排序,就需要通过转换为List集合然后使用工具类排序。
TreeMap<String, Teacher> tchMap = new TreeMap<String, Teacher>();
tchMap.put("1001",new Teacher("1001","小王",22,28020));
tchMap.put("1002",new Teacher("1002","小李",20,19555));
tchMap.put("1003",new Teacher("1003","小龙",25,15666));
tchMap.put("1004",new Teacher("1004","老黄",29,23552));
tchMap.put("1005",new Teacher("1005","老王",43,37555));
//通过将treeMap转换为list集合使用工具类进行排序
List<Map.Entry<String,Teacher>> list = new ArrayList<Map.Entry<String,Teacher>>(tchMap.entrySet());
//实现匿名比较器
Collections.sort(list,new Comparator<Map.Entry<String, Teacher>>() {
@Override
public int compare(Entry<String, Teacher> tch, Entry<String, Teacher> other) {
int flag = 0;
if(tch.getValue().getSalary()-other.getValue().getSalary()>0) {
flag = 1;
}else if(tch.getValue().getSalary()-other.getValue().getSalary()<0) {
flag = -1;
}
return flag;
}
});
for (Map.Entry<String,Teacher> map : list) {
System.out.print(map.getKey()+":"+map.getValue()+" ");
}
输出结果为
==============list集合遍历==================
[id=1002, name=小李, age=20, salary=19555.0] [id=1003, name=小龙, age=25, salary=15666.0] [id=1001, name=小王, age=25, salary=28020.0] [id=1004, name=老黄, age=29, salary=23552.0] [id=1005, name=老王, age=43, salary=37555.0]
==============Set集合遍历==================
[id=1002, name=小李, age=20, salary=19555.0] [id=1003, name=小龙, age=25, salary=15666.0] [id=1001, name=小王, age=25, salary=28020.0] [id=1004, name=老黄, age=29, salary=23552.0] [id=1005, name=老王, age=43, salary=37555.0]
==============Map集合遍历==================
1003:[id=1003, name=小龙, age=25, salary=15666.0] 1002:[id=1002, name=小李, age=20, salary=19555.0] 1004:[id=1004, name=老黄, age=29, salary=23552.0] 1001:[id=1001, name=小王, age=22, salary=28020.0] 1005:[id=1005, name=老王, age=43, salary=37555.0]