回答
Java 对 List 集合有如下几种排序方式:
- 使用
Collections.sort()
:该方法提供了两种排序方式,一种是自然排序,一种是使用 Comparator 比较器进行排序。 - 使用
List.sort()
:Java 8,在 List 接口中提供了sort(Comparator<? super E> c)
,允许我们直接对列表排序。 - 使用Stream API:利用 Stream 的
sorted()
可以对 List 中的元素进行排序。
详解
使用 Collections.sort()
Collections.sort()
有两个重载的方法。
- 自然排序
public static <T extends Comparable<? super T>> void sort(List<T> list)
该方法对列表中的元素按照它们的自然顺序进行排序,它要求列表中的元素实现了 Comparable
接口,并且相互之间可以进行比较。
@Data
@AllArgsConstructor
public class Student implements Comparable<Student> {
private String name;
private Integer age;
@Override
public int compareTo(Student student) {
int flag = this.age.compareTo(student.getAge());
if (flag == 0) {
flag = this.name.compareTo(student.getName());
}
return flag;
}
}
@Test
public void listSortTest() {
List<Student> students = new ArrayList<>();
students.add(new Student("张三",13));
students.add(new Student("李四",12));
students.add(new Student("王五",15));
students.add(new Student("赵六",14));
students.add(new Student("刘七",13));
Collections.sort(students);
System.out.println(students);
}
执行结果:
[Student(name=李四, age=12), Student(name=刘七, age=13), Student(name=张三, age=13), Student(name=赵六, age=14), Student(name=王五, age=15)]
注意,使用这种方式,List 中的元素一定要实现 Comparable 接口,否则会抛出 ClassCastException
异常。
- 定制排序
public static <T> void sort(List<T> list, Comparator<? super T> c)
根据指定的比较器对 List 进行排序。如果提供的比较器为null
,则列表中的元素将按照自然顺序排序。还是上的例子,这次我们按照 name 属性排序:
@Test
public void listSortTest() {
//...
Collections.sort(students, Comparator.comparing(Student::getName));
System.out.println(students);
}
执行结果:
[Student(name=刘七, age=13), Student(name=张三, age=13), Student(name=李四, age=12), Student(name=王五, age=15), Student(name=赵六, age=14)]
使用 List.sort()
Java 8 增加了一个默认方法 sort(Comparator<? super E> comparator)
,该方法允许直接在列表上进行排序操作,而无需使用Collections.sort()
。这种方式比 Collections.sort()
更加简洁。
方法接受一个Comparator
对象作为参数,用于比较列表中的元素。如果传入的Comparator
是null
,那么列表中的元素将按照它们的自然顺序进行排序,这就要求元素实现Comparable
接口,否则会抛出 ClassCastException
异常。
@Test
public void listSortTest() {
//...
students.sort(Comparator.comparing(Student::getAge));
System.out.println(students);
}
执行结果:
[Student(name=李四, age=12), Student(name=张三, age=13), Student(name=刘七, age=13), Student(name=赵六, age=14), Student(name=王五, age=15)]
使用Stream API
使用 Stream API 对 List 进行排序是一种更加方便的方式。如下:
@Test
public void listSortTest() {
//...
students.stream().sorted().forEach(System.out::println);
}
执行结果:
Student(name=李四, age=12)
Student(name=刘七, age=13)
Student(name=张三, age=13)
Student(name=赵六, age=14)
Student(name=王五, age=15)
这是一种自然排序的方式,也可以指定比较器:
@Test
public void listSortTest() {
//...
students.stream().sorted(Comparator.comparing(Student::getName)).forEach(System.out::println);
}
执行结果:
Student(name=刘七, age=13)
Student(name=张三, age=13)
Student(name=李四, age=12)
Student(name=王五, age=15)
Student(name=赵六, age=14)