JDK 8 Lambda expression for Java List de-duplication

JDK 8 provides Lambda expressions for streaming programming, and also provides a simple way to de-duplicate List.

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 5, 5, 5, 6, 7);

List<Integer> distinctNumbers = numbers.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(distinctNumbers);//1, 2, 3, 4, 5, 6, 7

It can be seen that Lambda expressions are indeed deduplicated. But what if it is to de-duplicate the attributes of a certain object?
For example:
we have an List<User>object like this, what should we do to conditionally de-duplicate User's name?

List<User> distinctUsers = users.stream()
.distinct(User::getName)
.collect(Collectors.toList());

But unfortunately, distinct does not provide construction parameters for conditional parameter passing, so there is a way to help, which is to define a filter first:

 private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
    
    
     Set<Object> seen = ConcurrentHashMap.newKeySet();
     return t -> seen.add(keyExtractor.apply(t));
 }

Then you can pass this filter in the distinct parameter

List<User> users = new LinkedList<>();
users.add(new User("Jim"));
users.add(new User("Jim"));
users.add(new User("Tom"));
users.add(new User("Leo"));

List<User> distinctUsers = users.stream()
        .filter(distinctByKey(User::getName))
        .collect(Collectors.toList());

System.out.println(distinctUsers);//[Jim, Tom, Leo]

Guess you like

Origin blog.csdn.net/JAYU_37/article/details/109805848