java8 利用 ConcurrentHashMap list根据 某个属性 去重
作者:互联网
//自定义方法
private static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
//使用
List<BindedNote> knowledgeList = page.getResult().stream().filter(distinctByKey(BindedNote::getNoteId)).collect(Collectors.toList());
标签:distinctByKey,ConcurrentHashMap,stream,自定义,list,keyExtractor,seen,java8 来源: https://www.cnblogs.com/wangfl/p/15330440.html