Comparable实现自定义排序的方式:使要比较的类实现这个接口即可
@Data
@ToString
public class Student implements Comparable<Student>{
private String name;
private int age;
private Date birth;
public Student(){
}
public Student(String name, int age, Date birth) {
super();
this.name = name;
this.age = age;
this.birth = birth;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
/**
比较规则:先比较出生日期,按升序排列,再比较年级,
按升序排列,最后比较名字,按降序排列
**/
@Override
public int compareTo(Student o) {
int result = 0;
result = this.birth.compareTo(o.birth);
if(0==result){
result = this.age-o.age;
if(result==0){
result = -this.name.compareTo(o.name);
}
}
return result;
}
}
外部调用排序:
Collections.sort(list);
Comparator的实现方式:不修改类的源码,新建一个类实现Comparator接口
@Data
@ToString
public class Goods {
private String goodsName;
private int price;
private int count;
public Goods() {
}
public Goods(String goodsName, int price, int count) {
super();
this.goodsName = goodsName;
this.price = price;
this.count = count;
}
实现比较器
public final class GoodsComp implements Comparator<Goods> {
private boolean query;
public GoodsComp(boolean query){
this.query = query;
}
@Override
public int compare(Goods o1, Goods o2) {
if(query){
return o1.getPrice()-o2.getPrice();
}else{
return o1.getCount()-o2.getCount(); }
}
}
这里加了一个boolean属性,用于指定排序规则是按price还是count属性进行,提高了可扩展性
外部调用比较器:假设按照price排序
Collections.sort(list,new GoodsComp (true));
两者的区别
-
Comparable是排序接口,若一个类实现了Comparable接口,就意味着“该类支持排序”。而Comparator是比较器,我们若需要控制某个类的次序,可以建立一个“该类的比较器”来进行排序。
-
Comparable相当于“内部比较器”,而Comparator相当于“外部比较器”。
-
两种方法各有优劣, 用Comparable 简单, 只要实现Comparable 接口的对象直接就成为一个可以比较的对象,但是需要修改源代码。 用Comparator 的好处是不需要修改源代码, 而是另外实现一个比较器, 当某个自定义的对象需要作比较的时候,把比较器和对象一起传递过去就可以比大小了, 并且在Comparator 里面用户可以自己实现复杂的可以通用的逻辑,使其可以匹配一些比较简单的对象,那样就可以节省很多重复劳动了。