Comparator 和 Comparable 两者都属于集合框架的一部分,都是在 JDK 1.2 时提供的用来在对象之间进行比较的接口,但两者又有些许的不同。
先来看一下 Comparable 的例子,定义实体类 Student,实现 Comparable,重写 compareTo 方法:
public class Student implements Comparable<Student> {
private String name;
private Integer age;
private Integer score;
public Student(String name, Integer age, Integer score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public int compareTo(Student o) {
return this.getName().compareTo(o.getName());
}
}
测试:
public static void main(String[] args) {
Student student1 = new Student("zhangsan", 1, 80);
Student student2 = new Student("lisi", 3, 90);
Student student3 = new Student("wangwu", 2, 100);
List<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
Collections.sort(list);
list.stream().forEach(n -> System.out.println(n.toString()));
}
// Student{name='lisi', age=3, score=90}
// Student{name='wangwu', age=2, score=100}
// Student{name='zhangsan', age=1, score=80}
从上面的例子我们大致了解了 Comparable 接口的使用,也就是说同一个类的对象之间如果要进行比较,需要实现 Comparable 接口,并且重写 compareTo 方法,这样比较的时候就会按照这个规则来进行比较并按照比较结果排序。
再来看一下 Comparator 的例子,定义实体类 Student,
public class Student {
private String name;
private Integer age;
private Integer score;
public Student(String name, Integer age, Integer score) {
this.name = name;
this.age = age;
this.score = score;
}
}
自定义比较器:
class AgeComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if (o1.getAge() > o2.getAge()) {
return 1;
} else if (o1.getAge() < o2.getAge()) {
return -1;
} else {
return 0;
}
}
}
class NameComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
}
测试:
public static void main(String[] args) {
Student student1 = new Student("zhangsan", 1, 80);
Student student2 = new Student("lisi", 3, 90);
Student student3 = new Student("wangwu", 2, 100);
List<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
// 这时候如果直接 Collections.sort(list) 会由于Student没有默认的自然排序,编译不过。
Collections.sort(list, new AgeComparator());
list.stream().forEach(n -> System.out.println(n.toString()));
System.out.println("\n-------------------");
Collections.sort(list, new NameComparator());
list.stream().forEach(n -> System.out.println(n.toString()));
}
先按照 AgeComparator 比较规则进行比较,再按照 NameComparator 比较器进行比较
Student{name='zhangsan', age=1, score=80}
Student{name='wangwu', age=2, score=100}
Student{name='lisi', age=3, score=90}
-------------------
Student{name='lisi', age=3, score=90}
Student{name='wangwu', age=2, score=100}
Student{name='zhangsan', age=1, score=80}
简单使用了两个类后,发现如果要对实体类的对象进行比较,在不修改原实体类的情况下,可以通过实现多个 Comparator 来实现多个比较规则。通过Comparator 我们可以自定义比较规则,针对对象的属性或者字段等来进行比较,而 Comparable 就实现不了,因为它的 compareTo 方法只能有一种比较规则。实现 Comparator 同样也要重写它的方法 compare。由于一般情况下我们实现的 Comparator 只有一个 compare 方法,所以我们可以对实现类进行一些优化:
- 使用匿名类来代替单独的实现类,比如我们可以将
Collections.sort(list, new NameComparator());
替换为:
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
});
- 借助 JDK 8 的 lambda 表达式,进一步优化:
Collections.sort(list, (o1, o2) -> o1.getName().compareTo(o2.getName()));
- 借助 JDK 8 中 Comparator 接口提供的新方法 comparing 再次优化:
Collections.sort(list, Comparator.comparing(Student::getName));
了解了他们的简单使用之后,我们可以来简单分析一下 Comparator 和 Comparable 的区别了。
- 相同点
- 两者都是用来用作对象之间的比较,都可以自定义比较规则;
- 两者都是返回一个描述对象之间关系的 int 值;
- 不同点
- 实现了 Comparable 的意思是
我可以把自己和另一个对象进行比较
;而实现了 Comparator 的意思是我可以比较其他两个对象
;也就是说 Comparable 是一个可比较的对象可以将自己与另一个对象进行比较;而 Comparator 是比较两个不同的对象。 - 使用 Comparable 需要修改原先的实体类,是属于一种自然排序。而 Comparator 则不用修改原先类。
- 即使修改了 Comparable 实体类,Comparable 也仅有一种比较规则。而 Comparator 可以实现多个,来提供多个比较规则。
- 实现了 Comparable 的意思是
下面来看一下各自的源码,由于都是接口,我们主要看下 JDK1.8 之后的默认实现方法。
Comparable
Comparable 比较简单,只有一个 compareTo 方法。我们重写该方法的时候注意一下对象的 NullPointerException 问题就可以了。
/**
*
* @see java.util.Comparator
* @since 1.2
*/
public interface Comparable<T> {
/**
* 将此对象与 order 的指定对象进行比较。当此对象小于、等于或大于指定对象时,返回负整数、零或正整数。
*/
public int compareTo(T o);
}
Comparator
Comparator 除了默认的 compare 和 equals 接口之外,其他的基本都是 JDK 1.8 提供的默认方法。
接下来大致介绍一下这些方法
reversed
返回逆序比较的比较器,底层直接使用 Collections 的 reverseOrder 来实现。
default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
thenComparing
这个方法是多条件排序的方法,当我们排序的条件不止一个的时候可以使用该方法。比如说我们对 Student 先按照 age 字段排序,再按照 score 排序,就可以使用 thenComparing 方法:
Student student1 = new Student("zhangsan", 1, 80);
Student student2 = new Student("lisi", 3, 90);
Student student3 = new Student("wangwu", 2, 100);
Student student4 = new Student("tom", 3, 75);
List<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
list.add(student4);
Collections.sort(list, Comparator.comparing(Student::getAge).thenComparing(Student::getScore));
list.stream().forEach(n -> System.out.println(n.toString()));
输出:
Student{name='zhangsan', age=1, score=80}
Student{name='wangwu', age=2, score=100}
Student{name='tom', age=3, score=75}
Student{name='lisi', age=3, score=90}
reverseOrder 和 naturalOrder
naturalOrder 是返回自然排序的比较器,reverseOrder 和 naturalOrder 相反,两者都是用于返回实现了 Comparable 接口的对象的比较器。我们借助刚才 Comparator 和 Comparable 两者进行比较时的 Comparator 的代码,先看一下自然顺序:
Collections.sort(list, Comparator.naturalOrder());
输出,结果和原来一样:
Student{name='lisi', age=3, score=90}
Student{name='wangwu', age=2, score=100}
Student{name='zhangsan', age=1, score=80}
再看一下逆序:
Collections.sort(list, Comparator.reverseOrder());
输出:
Student{name='zhangsan', age=1, score=80}
Student{name='wangwu', age=2, score=100}
Student{name='lisi', age=3, score=90}
这两个方法说白了就是将 Comparable 的方式转换为 Comparator,因为 Comparable 的功能有限,不方便我们基于 Comparable 进行扩展。底层实现分别借助于工具类 Collections 及 Comparators 来实现。Comparators 是专门用于支持 Comparator 的内部类。
public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {
return Collections.reverseOrder();
}
public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE;
}
nullsFirst 和 nullsLast
这两个方法是说如果排序的字段为 null 的情况下这条记录怎么排序。nullsFirst 是说将这条记录排在最前面,而 nullsLast 是说将这条记录排序在最后面。
public static void main(String[] args) {
Student student1 = new Student("zhangsan", 1, 80);
Student student2 = new Student("lisi", null, 90);
Student student3 = new Student("wangwu", 2, 100);
List<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
Comparator<Student> comparator = Comparator.comparing(Student::getAge,
Comparator.nullsLast(Comparator.reverseOrder()));
Collections.sort(list, comparator);
list.stream().forEach(n -> System.out.println(n.toString()));
}
按照 age 进行逆序排列,将 key 为 null 的排到最后面
Student{name='wangwu', age=2, score=100}
Student{name='zhangsan', age=1, score=80}
Student{name='lisi', age=null, score=90}
按照 age 进行自然顺序排列,将 key 为 null 的排再最前面
Comparator<Student> comparator = Comparator.comparing(Student::getAge,
Comparator.nullsFirst(Comparator.naturalOrder()));
Collections.sort(list, comparator);
输出:
Student{name='lisi', age=null, score=90}
Student{name='zhangsan', age=1, score=80}
Student{name='wangwu', age=2, score=100}
如果多个 key 都为 null 的话,那将无法保证这几个对象的排序。源码很简单,直接通过 Comparators 内部类实现的。
public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) {
return new Comparators.NullComparator<>(true, comparator);
}
public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) {
return new Comparators.NullComparator<>(false, comparator);
}
comparing
comparing 方法就是获取对象的比较器也就是比较规则,有几个重载方法及对应类型的方法,第一个参数接受的是函数式表达式。我们使用例子来看下,修改原先 Student 类,添加一个布尔类型字段 likeGame,然后测试
public static void main(String[] args) {
List<Student> list = Arrays.asList(
new Student("zhangsan", 1, 80, true),
new Student("wangwu", 3, 100, false),
new Student("zisi", 4, 110, true),
new Student("zisi", 2, 110, true)
);
Collections.sort(list, Comparator.comparing(Student::getLikeGame).thenComparing(Student::getAge));
list.stream().forEach(n -> System.out.println(n.toString()));
}
输出:
Student{name='wangwu', age=3, score=100, likeGame=false}
Student{name='zhangsan', age=1, score=80, likeGame=true}
Student{name='zisi', age=2, score=110, likeGame=true}
Student{name='zisi', age=4, score=110, likeGame=true}
这就实现了按照 Student 对象的 likeGame 进行自然排序,同样,两个参数的接口,第二个参数可以指定具体的比较规则:
Collections.sort(list, Comparator.comparing(Student::getLikeGame, Comparator.reverseOrder())
.thenComparing(Student::getAge));
list.stream().forEach(n -> System.out.println(n.toString()));
这就实现了按照逆序对 likeGame 进行排序,输出:
Student{name='zhangsan', age=1, score=80, likeGame=true}
Student{name='zisi', age=2, score=110, likeGame=true}
Student{name='zisi', age=4, score=110, likeGame=true}
Student{name='wangwu', age=3, score=100, likeGame=false}