美文网首页
Java8对list集合进行排序、过滤、分组、去重、转map、遍

Java8对list集合进行排序、过滤、分组、去重、转map、遍

作者: rainbowz | 来源:发表于2020-08-30 12:06 被阅读0次

// 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));

https://www.e-learn.cn/topic/2522732

[https://www.dazhuanlan.com/2019/12/13/5df2e5d192fcc/]

相关文章

网友评论

      本文标题:Java8对list集合进行排序、过滤、分组、去重、转map、遍

      本文链接:https://www.haomeiwen.com/subject/wktesktx.html