杰瑞科技汇

Java List对象如何实现排序?

Java List 对象排序指南

在 Java 中对 List 中的对象进行排序有几种常见方法,下面我将详细介绍这些方法。

使用 Comparable 接口

如果对象本身具有自然排序顺序,可以实现 Comparable 接口。

public class Person implements Comparable<Person> {
    private String name;
    private int age;
    // 构造方法、getter和setter省略
    @Override
    public int compareTo(Person other) {
        return this.name.compareTo(other.name); // 按名字排序
        // 或者按年龄排序: return Integer.compare(this.age, other.age);
    }
}
// 使用
List<Person> people = new ArrayList<>();
// 添加元素...
Collections.sort(people); // 按照compareTo方法定义的顺序排序

使用 Comparator 接口

如果不想修改类本身或需要多种排序方式,可以使用 Comparator

1 匿名内部类方式

List<Person> people = new ArrayList<>();
// 添加元素...
Collections.sort(people, new Comparator<Person>() {
    @Override
    public int compare(Person p1, Person p2) {
        return p1.getName().compareTo(p2.getName()); // 按名字排序
    }
});

2 Java 8+ Lambda 表达式

List<Person> people = new ArrayList<>();
// 添加元素...
// 按名字排序
Collections.sort(people, (p1, p2) -> p1.getName().compareTo(p2.getName()));
// 按年龄排序
Collections.sort(people, (p1, p2) -> Integer.compare(p1.getAge(), p2.getAge()));

3 Java 8+ 方法引用

List<Person> people = new ArrayList<>();
// 添加元素...
// 按名字排序
Collections.sort(people, Comparator.comparing(Person::getName));
// 按年龄排序
Collections.sort(people, Comparator.comparingInt(Person::getAge));

4 多条件排序

// 先按年龄升序,年龄相同再按名字降序
Collections.sort(people, 
    Comparator.comparingInt(Person::getAge)
              .thenComparing(Person::getName, Comparator.reverseOrder()));

使用 Stream API (Java 8+)

List<Person> sortedPeople = people.stream()
    .sorted(Comparator.comparing(Person::getName)) // 按名字排序
    .collect(Collectors.toList());

使用 List.sort() 方法 (Java 8+)

List<Person> people = new ArrayList<>();
// 添加元素...
// 按名字排序
people.sort(Comparator.comparing(Person::getName));

降序排序

// 使用reversed()方法
Collections.sort(people, Comparator.comparing(Person::getName).reversed());
// 或者直接比较反转
Collections.sort(people, (p1, p2) -> p2.getName().compareTo(p1.getName()));

注意事项

  1. 排序是原地操作,会修改原始 List
  2. List 为 null,排序会抛出 NullPointerException
  3. 自定义比较器应确保比较逻辑的一致性(自反性、对称性、传递性)
  4. 对于大型 List,考虑使用 List.parallelStream() 进行并行排序

示例代码

import java.util.*;
public class PersonSortExample {
    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
            new Person("Alice", 30),
            new Person("Bob", 25),
            new Person("Charlie", 35),
            new Person("David", 25)
        );
        // 按名字排序
        people.sort(Comparator.comparing(Person::getName));
        System.out.println("按名字排序: " + people);
        // 按年龄排序
        people.sort(Comparator.comparingInt(Person::getAge));
        System.out.println("按年龄排序: " + people);
        // 先按年龄升序,年龄相同再按名字降序
        people.sort(Comparator.comparingInt(Person::getAge)
                              .thenComparing(Person::getName, Comparator.reverseOrder()));
        System.out.println("多条件排序: " + people);
    }
}
class Person {
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    @Override
    public String toString() {
        return name + "(" + age + ")";
    }
}

就是在 Java 中对 List 对象进行排序的各种方法,你可以根据具体需求选择最适合的方式。

分享:
扫描分享到社交APP
上一篇
下一篇