首页 > 技术文章 > list一个字段去重

huqi96 2020-11-15 17:00 原文

  //按照某个字段去重
    public 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<IntegralCountVo> lists = list.stream().filter(distinctByKey(b -> b.getCompanyId())).collect(Collectors.toList());

 

推荐阅读