Java8 list根据对象某个属性去重
1. 添加方法:
import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; 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; }
2. 使用:
facultyRetList = facultyList.stream().filter(distinctByKey(Faculty::getId)).collect(Collectors.toList());
文章来自:https://www.cnblogs.com/miaoying/p/9542002.html