例子
实体类
Demo1
代码
// 根据id去重
list = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(Person::getPackId))),ArrayList::new));
结果
Demo2
代码
// 根据id和ctn去重
list = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(t -> t.getPackId() + "#" + t.getActCtn()))),ArrayList::new));
结果
注意
当使用多个字段进行去重时,中间用 “#” 连接即可