Java8对list集合进行排序、过滤、分组、去重、转map、遍
2020-08-30 本文已影响0人
rainbowz
// xxx 表示你需要去重的字段 列如(o -> o.id()) 返回已经去重集合
List<AddEventAndProperty> nameDistinct = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> xxx))), ArrayList::new));
// 通过多个字段去重,返回已经去重集合
List<AddEventAndProperty> distinctClass = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.id() + ";" + o.getName()))), ArrayList::new));
// 根据scId去重
ArrayList<Quota> collect = tickets.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparingLong(Quota::getScId))), ArrayList::new));