原文链接:https://blog.csdn.net/qq_39940205/article/details/114269686
_______________________________________________________________________________________________________________
我们知道, Java8 lambda自带的去重为 distinct 方法, 但是只能过滤整体对象, 不能实现对象里的某个值进行判定去重, 比如:
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
但是, 如果我们有一个 List<User> 类似这样的对象, 要对 User 的 name 进行条件去重怎么办?
我们想要的效果是这样的:
List<User> distinctUsers = users.stream() .distinct(User::getName) .collect(Collectors.toList());
但是很遗憾, distinct()方法并不能设置条件. 解决方案如下:
首先定义一个过滤器:
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; }
然后就可以进行条件去重啦:
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]
再来看比较炫酷的Java8写法:
import static java.util.Comparator.comparingLong; import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toCollection; // 根据id去重 List<Person> unique = persons.stream().collect( collectingAndThen( toCollection(() -> new TreeSet<>(comparingLong(Person::getId))), ArrayList::new) );
标签:users,stream,distinct,List,User,new,Lambda From: https://www.cnblogs.com/kelelipeng/p/18047735