List deduplication based on fields

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));

https://blog.csdn.net/qq_45932382/article/details/122925765

Guess you like

Origin blog.csdn.net/weixin_42151235/article/details/130269388