集合listUsers
方法1:
List<Users> distinctUsers = listUsers.stream()
.collect(Collectors.collectingAndThen
(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(t -> t.getSno()))), ArrayList::new)
);
方法2:
List<Users> distinctUsers = listUsers.stream().filter(distinctByKey(Users::getSno)).collect(Collectors.toList());
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null;
}