一、原生的distinct()不支持按照列表里的对象某个属性去重

 

二、对某个字段过滤重复数据:使用HashMap

private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
list.stream().filter(distinctByKey(User::getId)).collect(Collectors.toList());

 

 

 

参考:

https://www.cnblogs.com/unknows/p/13534953.html

 

相关文章:

  • 2021-11-18
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
  • 2021-08-10
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案