Java 8 Stream.distinct() 列表去重示例
在这篇文章里,我们将提供Java8 Stream distinct()示例。 distinct()返回由该流的不同元素组成的流。distinct()是Stream接口的方法。distinct()使用hashCode()和equals()方法来获取不同的元素。因此,我们的类必须实现hashCode()和equals()方法。如果distinct()正在处理有序流,那么对于重复元素,将保留以遭遇顺序首先出现的元素,并且以这种方式选择不同元素是稳定的。在无序流的情况下,不同元素的选择不一定是稳定的,是可以改变的。distinct()执行有状态的中间操作。在有序流的并行流的情况下,保持distinct()的稳定性是需要很高的代价的,因为它需要大量的缓冲开销。如果我们不需要保持遭遇顺序的一致性,那么我们应该可以使用通过BaseStream.unordered()方法实现的无序流。
1. Stream.distinct()
Stream<T> distinct()
它是Stream接口的方法。在此示例中,我们有一个包含重复元素的字符串数据类型列表
DistinctSimpleDemo.java
-
package com.concretepage;
-
import java.util.Arrays;
-
import java.util.List;
-
import java.util.stream.Collectors;
-
public class DistinctSimpleDemo {
-
public static void main(String[] args) {
-
List<String> list = Arrays.asList("AA", "BB", "CC", "BB", "CC", "AA", "AA");
-
long l = list.stream().distinct().count();
-
System.out.println("No. of distinct elements:"+l);
-
String output = list.stream().distinct().collect(Collectors.joining(","));
-
System.out.println(output);
-
}
-
}
-
No. of distinct elements:3
-
AA,BB,CC
2. Stream.distinct() with List of Objects
在此示例中,我们有一个Book对象列表。 为了对列表进行去重,该类将重写hashCode()和equals()。
Book.java
-
package com.concretepage;
-
public class Book {
-
private String name;
-
private int price;
-
public Book(String name, int price) {
-
this.name = name;
-
this.price = price;
-
}
-
public String getName() {
-
return name;
-
}
-
public int getPrice() {
-
return price;
-
}
-
-
public boolean equals(final Object obj) {
-
if (obj == null) {
-
return false;
-
}
-
final Book book = (Book) obj;
-
if (this == book) {
-
return true;
-
} else {
-
return (this.name.equals(book.name) && this.price == book.price);
-
}
-
}
-
-
public int hashCode() {
-
int hashno = 7;
-
hashno = 13 * hashno + (name == null ? 0 : name.hashCode());
-
return hashno;
-
}
-
}
-
package com.concretepage;
-
import java.util.ArrayList;
-
import java.util.List;
-
public class DistinctWithUserObjects {
-
public static void main(String[] args) {
-
List<Book> list = new ArrayList<>();
-
{
-
list.add(new Book("Core Java", 200));
-
list.add(new Book("Core Java", 200));
-
list.add(new Book("Learning Freemarker", 150));
-
list.add(new Book("Spring MVC", 300));
-
list.add(new Book("Spring MVC", 300));
-
}
-
long l = list.stream().distinct().count();
-
System.out.println("No. of distinct books:"+l);
-
list.stream().distinct().forEach(b -> System.out.println(b.getName()+ "," + b.getPrice()));
-
}
-
}
-
No. of distinct books:3
-
Core Java,200
-
Learning Freemarker,150
-
Spring MVC,300
3. Distinct by Property
distinct()不提供按照属性对对象列表进行去重的直接实现。它是基于hashCode()和equals()工作的。如果我们想要按照对象的属性,对对象列表进行去重,我们可以通过其它方法来实现。如下代码段所示:
-
static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
-
Map<Object,Boolean> seen = new ConcurrentHashMap<>();
-
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
-
}
list.stream().filter(distinctByKey(b -> b.getName()));
distinctByKey()方法返回一个使用ConcurrentHashMap 来维护先前所见状态的 Predicate 实例,如下是一个完整的使用对象属性来进行去重的示例。
DistinctByProperty.java
-
package com.concretepage;
-
import java.util.ArrayList;
-
import java.util.List;
-
import java.util.Map;
-
import java.util.concurrent.ConcurrentHashMap;
-
import java.util.function.Function;
-
import java.util.function.Predicate;
-
public class DistinctByProperty {
-
public static void main(String[] args) {
-
List<Book> list = new ArrayList<>();
-
{
-
list.add(new Book("Core Java", 200));
-
list.add(new Book("Core Java", 300));
-
list.add(new Book("Learning Freemarker", 150));
-
list.add(new Book("Spring MVC", 200));
-
list.add(new Book("Hibernate", 300));
-
}
-
list.stream().filter(distinctByKey(b -> b.getName()))
-
.forEach(b -> System.out.println(b.getName()+ "," + b.getPrice()));
-
}
-
private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
-
Map<Object,Boolean> seen = new ConcurrentHashMap<>();
-
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
-
}
-
}
-
Core Java,200
-
Learning Freemarker,150
-
Spring MVC,200
-
Hibernate,300
posted on 2022-06-17 17:22 ExplorerMan 阅读(331) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2019-06-17 redis为什么内存不宜过大
2019-06-17 Python 操作 mongodb 亿级数据量使用 Bloomfilter 高效率判断唯一性 例子
2019-06-17 Redis-3.2.0集群配置(redis cluster)
2019-06-17 在reshard过程中,将会询问reshard多少slots:
2019-06-17 Redis 持久化之RDB和AOF
2019-06-17 Redis "MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk"问题的解决
2019-06-17 Centos6.9下RabbitMQ集群部署记录