Java Stream.distinct()
Java 中的 Stream.distinct() 方法用于从流中过滤掉重复元素,确保生成的流仅包含唯一元素。它基于流中对象的 equals() 方法工作。
此方法是 Java 8 中引入的 Java Stream API 的一部分,通常用于处理具有重复值的集合或数组。
示例 1:从字符串列表中删除重复项
想象一下你有一个名字列表,其中一些名字是重复的。您想要一个唯一名称的列表。
import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { // List of names with duplicates List<String> names = List.of("Alice", "Bob", "Alice", "Charlie", "Bob", "David"); // Use Stream.distinct() to remove duplicates List<String> uniqueNames = names.stream() .distinct() .collect(Collectors.toList()); System.out.println(uniqueNames); // Output: [Alice, Bob, Charlie, David] } }
工作原理:
distinct() 方法比较每个名称并仅保留第一次出现的重复项。
示例 2:从数字列表中删除重复项
让我们列出存在重复的数字并仅提取唯一的数字。
import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { // List of numbers with duplicates List<Integer> numbers = List.of(1, 2, 3, 2, 4, 3, 5); // Use Stream.distinct() to remove duplicates List<Integer> uniqueNumbers = numbers.stream() .distinct() .collect(Collectors.toList()); System.out.println(uniqueNumbers); // Output: [1, 2, 3, 4, 5] } }
工作原理:
使用数字的自然相等性进行比较(基于整数的 equals()),因此重复项会被过滤掉。
示例 3:使用简单对象
让我们创建一个 Product 类并根据产品的 id 删除重复项。
代码:
import java.util.List; import java.util.Objects; import java.util.stream.Collectors; class Product { private int id; private String name; public Product(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Product product = (Product) o; return id == product.id; } @Override public int hashCode() { return Objects.hash(id); } @Override public String toString() { return "Product{id=" + id + ", name='" + name + "'}"; } } public class Main { public static void main(String[] args) { // List of products with duplicates (based on id) List<Product> products = List.of( new Product(1, "Laptop"), new Product(2, "Tablet"), new Product(1, "Laptop"), // Duplicate new Product(3, "Smartphone"), new Product(2, "Tablet") // Duplicate ); // Use Stream.distinct() to remove duplicates List<Product> uniqueProducts = products.stream() .distinct() .collect(Collectors.toList()); System.out.println(uniqueProducts); // Output: // [Product{id=1, name='Laptop'}, Product{id=2, name='Tablet'}, Product{id=3, name='Smartphone'}] } }
说明:
相等性检查:重写 equals() 方法以确保 Product 对象根据其 id 被视为相等。
去重:当distinct()遇到两个具有相同id的产品时,只保留第一个。
输出:您获得独特产品的列表。
简化理解
- 对于原始或简单对象(如整数、字符串):
distinct() 通过直接比较值来删除重复项。
示例:[1, 2, 2, 3] 变为 [1, 2, 3]。
- 对于自定义对象:
您需要为类实现 equals() 和 hashCode() 方法。
distinct() 使用这些方法来确定两个对象是否重复。
用例:过滤用户输入中的重复名称
想象一下,用户在表单中重复输入名称,并且您希望确保仅存储唯一的名称。
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { // Simulate user input List<String> userInput = new ArrayList<>(); userInput.add("John"); userInput.add("Mary"); userInput.add("John"); // Duplicate userInput.add("Alice"); // Remove duplicates List<String> uniqueInput = userInput.stream() .distinct() .collect(Collectors.toList()); System.out.println(uniqueInput); // Output: [John, Mary, Alice] } }
这确保应用程序仅存储唯一的名称,而不需要手动检查重复项。
最后的收获:
distinct() 很简单:它从流中删除重复项。
对于基元或不可变类型:直接使用即可。
对于自定义对象:确保正确的 equals() 和 hashCode() 实现。
实用提示:用它来清理任何形式的重复数据(例如列表、用户输入、数据库结果)。
以上是Java Stream.distinct()的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

在使用IntelliJIDEAUltimate版本启动Spring...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

在使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名以构建查询条件,是一个常见的难题。本文将针...
