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));
相关文章
- 10-30stream流中根据单条件或多条件过滤