stream流中根据单条件或多条件过滤去重

public 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(a->a.getParam));
posted @ 2021-09-03 12:27  DarkerbeS  阅读(1103)  评论(0编辑  收藏  举报