Java里如何使用distinct去重集合元素_Stream去重操作解析

distinct()方法基于equals()和hashCode()去重,对基本类型和字符串直接有效,自定义对象需重写这两个方法;按字段去重可用Collectors.toMap()或辅助Set实现。

在Java中,使用Streamdistinct()方法可以方便地对集合元素进行去重操作。这个方法基于元素的equals()hashCode()方法判断重复,适用于基本类型、字符串以及自定义对象。

distinct() 方法原理

distinct()Stream接口中的一个中间操作,它返回一个由不同元素组成的流,即去除重复项。其内部通过维护一个Set来记录已见过的元素,确保每个元素只出现一次。

去重的关键在于:

  • 对于String、Integer等包装类型,直接使用它们重写过的equals()hashCode()方法判断是否重复。
  • 对于自定义对象,必须正确重写equals()hashCode()方法,否则无法识别逻辑上的重复对象。

对基本类型和字符串去重

处理简单数据类型时,distinct()可以直接使用,无需额外配置。

示例:去除整数列表中的重复值

List numbers = Arrays.asList(1, 2, 2, 3, 3, 3, 4);
List unique = numbers.stream()
                              .distinct()
                              .collect(Collectors.toList());
System.out.println(unique); // 输出 [1, 2, 3, 4]

示例:去除字符串列表中的重复项

List words = Arrays.asList("apple", "banana", "apple", "orange", "banana");
List uniqueWords = words.stream()
                                .distinct()
                                .collect(Collectors.toList());
System.out.println(uniqueWords); // 输出 [apple, banana, orange]

对自定义对象去重

当处理自定义类的对象时,如果未重写equals()hashCode(),即使两个对象内容相同,也会被视为不同对象。

例如有一个User类:

class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 必须重写 equals 和 hashCode
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;
        User user = (User) o;
        return age == user.age && Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

然后进行去重操作:

List users = Arrays.asList(
    new User("Alice", 25),
    new User("Bob", 30),
    new User("Alice", 25)
);

List uniqueUsers = users.stream()
                              .distinct()
                              .collect(Collectors.toList());

System.out.println(uniqueUsers.size()); // 输出 2

因为重写了equals()hashCode(),所以两个同名同年龄的用户被视为重复。

按对象某个字段去重(进阶用法)

有时我们不希望依赖equals(),而是根据对象的某个属性去重,比如只按用户名去重。这时可以通过TreeMap或辅助Set实现。

使用Collectors.toMap()结合LinkedHashMap保留顺序:

list.stream()
   .collect(Collectors.collectingAndThen(
       Collectors.toMap(
           User::getName,  // 按name作为key
           u -> u,
           (existing, replacement) -> existing, // 保留第一个
           LinkedHashMap::new
       ),
       map -> new ArrayList<>(map.values())
   ));

或者使用辅助Set记录已出现的字段值:

Set seen = new HashSet<>();
List distinctByName = users.stream()
    .filter(user -> seen.add(user.getName())) // add返回false表示已存在
    .collect(Collectors.toList());

基本上就这些。只要理解distinct()依赖equals/hashCode,并在需要时通过辅助手段实现字段级去重,就能灵活应对各种场景。