一、原生的distinct()不支持按照列表里的對象某個屬性去重
二、對某個字段過濾重復數據:使用HashMap
private 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(User::getId)).collect(Collectors.toList());
參考:
https://www.cnblogs.com/unknows/p/13534953.html
