Java8对list集合进行排序、过滤、分组、去重、转map、遍历赋值等操作

// xxx 表示你需要去重的字段 列如(o -> o.id()) 返回已经去重集合

List nameDistinct = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> xxx))), ArrayList::new));

// 通过多个字段去重,返回已经去重集合
List distinctClass = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.id() + ";" + o.getName()))), ArrayList::new));

// 根据scId去重
ArrayList 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、遍历赋值等操作)