List对象里面根据某一字段去重
时间:2020-08-13 12:13:09
收藏:0
阅读:452
Person类
// 根据name去重
List<Person> unique = persons.stream().collect( Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new) );
这样会留下一个唯一姓名的,就比如原来List中有三个张三,会留下一个name = 张三的。用的是TreeSet集合保存。
原文:https://www.cnblogs.com/onlyzhangmeng/p/13494782.html
评论(0)