1. Use toCollection and TreeSet to deduplicate
TreeSet uses TreeMap internally , using the specified Comparator to compare elements, and if the elements are the same, the new element replaces the old element.
// 1. 某个字段去重
List<User> collect = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()-> new TreeSet<>(Comparator.comparing(User :: getUserId))), ArrayList::new));
// 2. 多个字段去重
List<User> collect =
userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getSex() + "#" + o.getAddre()))), ArrayList::new));
2. Use Collectors.toMap to deduplicate
Collectors.toMap needs to use a version with three parameters. The first two parameters are the keyMapper function and the other is the valueMapper function. The third parameter is the BinaryOperator function interface. The BinaryOperator function receives two parameters, one oldValue and one newValue. Used for data processing when the key is repeated
List<User> collect = new ArrayList<>(userList.stream().collect(Collectors.toMap(User::getUserId, Function.identity(), (oldValue, newValue) -> oldValue)).values());
3. Use Collectors.groupingBy to deduplicate groups
Group a field to deduplicate
Map<String, List<User>> map = lists.stream().collect(Collectors.groupingBy(User::getAddre));