List对象里面根据某一字段去重

Person类

// 根据name去重

List<Person> uniquePersons = persons.stream().collect( Collectors.collectingAndThen(

Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new) );

这样会留下一个唯一姓名的,就比如原来List中有三个张三,会留下一个name = 张三的。用的是TreeSet集合保存。

要是想拿到去除的数据,可以用原来的persons.remaveAll(uniquePersons);

传送门:

https://blog.csdn.net/ianly123/article/details/82658622?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-5.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-5.channel_param

朴实的代码:https://www.cnblogs.com/cainiao-Shun666/p/7911142.html

下班记得打卡
原文地址:https://www.cnblogs.com/onlyzhangmeng/p/13494782.html