JDK1.8stream根据对象的某一字段去重
代码语言:javascript复制List list = new ArrayList<>();
List listByName = list.stream().filter(distinctByName(item -> item.getName())).collect(Collectors.toList());
private static <T> Predicate<T> distinctByName(Function<? super T , Object> keyExtractor) {
Map<Object , Boolean> seen = new ConcurrentHaskMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t)) , Boolean.True) == null;
}