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

在Java中,使用Stream的distinct()方法可以方便地对集合元素进行去重操作。这个方法基于元素的equals()和hashCode()方法判断重复,适用于基本类型、字符串以及自定义对象。
distinct()是Stream接口中的一个中间操作,它返回一个由不同元素组成的流,即去除重复项。其内部通过维护一个Set来记录已见过的元素,确保每个元素只出现一次。
去重的关键在于:
equals()和hashCode()方法判断是否重复。equals()和hashCode()方法,否则无法识别逻辑上的重复对象。处理简单数据类型时,distinct()可以直接使用,无需额外配置。
立即学习“Java免费学习笔记(深入)”;
示例:去除整数列表中的重复值
List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 3, 3, 4);
List<Integer> unique = numbers.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(unique); // 输出 [1, 2, 3, 4]
示例:去除字符串列表中的重复项
List<String> words = Arrays.asList("apple", "banana", "apple", "orange", "banana");
List<String> 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<User> users = Arrays.asList(
new User("Alice", 25),
new User("Bob", 30),
new User("Alice", 25)
);
List<User> 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<String> seen = new HashSet<>();
List<User> distinctByName = users.stream()
.filter(user -> seen.add(user.getName())) // add返回false表示已存在
.collect(Collectors.toList());
基本上就这些。只要理解distinct()依赖equals/hashCode,并在需要时通过辅助手段实现字段级去重,就能灵活应对各种场景。
以上就是Java里如何使用distinct去重集合元素_Stream去重操作解析的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号