list去重,分组

java8对List<Bean>进行去重和覆盖

不关心覆盖逻辑,相同personId只留一条

public static List<Person> coverDuplicate(List<Person> sourceList) {
  if (CollectionUtils.isEmpty(sourceList)) {
    return new ArrayList<>();
  }
  List<Person> distinctList = sourceList.stream().collect(
    Collectors.collectingAndThen(
        Collectors.toCollection(
           () -> new TreeSet<>(Comparator.comparing(o -> o.getPersonId()))), ArrayList::new)
  );
  return distinctList;
}

相同的personId,后面的记录要求覆盖前面的

public static List<Person> coverDuplicate1(List<Person> sourceList) {
  if (CollectionUtils.isEmpty(sourceList)) {
    return new ArrayList<>();
  }
  List<Person> distinctList = sourceList.stream().collect(
    Collectors.toMap(Person::getPersonId, Function.identity(), (e1, e2) -> e2)
      ).values().stream().collect(Collectors.toList());
  return distinctList;
}

list string去重

1、java8

list.stream().distinct().collect(Collectors.toList());

2、借助Set的特性进行去重,由于Set的无序性,不会保持原来顺序

/**
     * 去除重复数据
     * @param list
     */
    public static List<String> list distinct(List<String> list) {
        final boolean sta = null != list && list.size() > 0;
        List doubleList= new ArrayList();
        if (sta) {
            Set set = new HashSet();
            set.addAll(list);
            doubleList.addAll(set);
        }
        return doubleList;
    }

3、利用set集合特性保持顺序一致去重

// Set去重并保持原先顺序的两种方法
   public static void delRepeat(List<String> list) {
       //方法一
       List<String> listNew = new ArrayList<String>(new TreeSet<String>(list));
       //方法二
       List<String> listNew2 = new ArrayList<String>(new LinkedHashSet<String>(list));
   }

4、分组求个数

/jdk8的方法统计个数:

Map> map = list.stream().collect(Collectors.groupingBy(User::getUserName,Collectors.counting()));

5、分组求和

studentList.stream().collect(Collectors.toMap(Student::getName, Student::getScore, Integer::sum));

List<student> studentList = new ArrayList<>();
studentList.stream()
       .collect(Collectors.groupingBy(Student::getName, Collectors.collectingAndThen(
           Collectors.summarizingDouble(Student::getScore), DoubleSummaryStatistics::getSum)));

List<student> studentList = new ArrayList<>();
studentList.stream()
       .collect(Collectors.groupingBy(Student::getName, Collectors.collectingAndThen(
            Collectors.mapping(Student::getScore, Collectors.reducing(Integer::sum)),
            Optional::get)));
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容