Java.util 包中的 List 接口继承了 Collection 接口,用来存放对象集合,当对这些对象进行排序的时候,要么让对象类自己实现同类对象的比较,要么借助比较器进行比较排序。下面几个示例中对年龄进行排序:
实现 Comparable 接口
public class AppDemo {
public static void main(String[] args) {
Student student1 = new Student("张三", 20);
Student student2 = new Student("李四", 25);
Student student3 = new Student("王五", 22);
Student student4 = new Student("赵六", 21);
Student student5 = new Student("钱七", 23);
Student student6 = new Student("孙八", 24);
List<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
list.add(student4);
list.add(student5);
list.add(student6);
System.out.println("排序前:" + list);
Collections.sort(list);
System.out.println("排序后:" + list);
}
}
@Data
@AllArgsConstructor
class Student implements Comparable<Student> {
private String name;
private Integer age;
@Override
public int compareTo(Student o) {
return this.age.compareTo(o.age);
}
}
结果
排序前:[Student(name=张三, age=20), Student(name=李四, age=25), Student(name=王五, age=22), Student(name=赵六, age=21), Student(name=钱七, age=23), Student(name=孙八, age=24)]
排序后:[Student(name=张三, age=20), Student(name=赵六, age=21), Student(name=王五, age=22), Student(name=钱七, age=23), Student(name=孙八, age=24), Student(name=李四, age=25)]
使用 Comparator 排序
public class AppDemo {
public static void main(String[] args) {
Student student1 = new Student("张三", 20);
Student student2 = new Student("李四", 25);
Student student3 = new Student("王五", 22);
Student student4 = new Student("赵六", 21);
Student student5 = new Student("钱七", 23);
Student student6 = new Student("孙八", 24);
List<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
list.add(student4);
list.add(student5);
list.add(student6);
System.out.println("排序前:" + list);
// Collections.sort(list, Comparator.comparing(Student::getAge));
Collections.sort(list, (o1, o2) -> {
return o1.getAge().compareTo(o2.getAge());
});
System.out.println("排序后:" + list);
}
}
@Data
@AllArgsConstructor
class Student {
private String name;
private Integer age;
使用 Stream 排序
底层还是通过 Comparable 实现的
public class AppDemo {
public static void main(String[] args) {
Student student1 = new Student("张三", 20);
Student student2 = new Student("李四", 25);
Student student3 = new Student("王五", 22);
Student student4 = new Student("赵六", 21);
Student student5 = new Student("钱七", 23);
Student student6 = new Student("孙八", 24);
List<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
list.add(student4);
list.add(student5);
list.add(student6);
System.out.println("排序前:" + list);
// 如果Student实现了Comparable
// List<Student> list2 = list.stream().sorted().collect(Collectors.toList());
// 如果Student没有实现Comparable
List<Student> list2 = list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
System.out.println("排序后:" + list2);
}
}
@Data
@AllArgsConstructor
class Student {
private String name;
private Integer age;
}
有了Comparable为什么还需要Comparator?
Comparable用于使某个类具备可排序能力。如之前的Student类,实现该接口后覆盖其compareTo方法,即可具备可排序的能力。
但是仍然存在一些二方库的类没有实现Comparable,但是调用方也需要比较的,此时就需要使用Comparator接口。
Comparator是一个比较器接口,可以用来给不具备排序能力的对象进行排序。