java8中List根据某一属性去重

本文介绍了一种在为创新创业孵化园开发Web管理平台时遇到的需求场景:如何针对多个工位查询结果进行去重处理,仅保留每个公司的所在园区信息。通过使用Java 8 Stream API结合Collectors工具类实现对特定字段的去重。

最近再给一个创新创业孵化园做一个wab管理平台,项目中有这么一个需求:每一个入孵入驻的公司需要选择工位,后台管理列表需要通过查询工位来确定这个公司现在入驻在哪个园区中,一个公司不可能只有一个工位,所以检索工位的话会查出来很多条记录,但是园区这个字段都是一样的,我只需要知道是哪个园区就好了,这时候就需要给查出来的List去重了。
这里写图片描述
这里写图片描述

关键代码:
cpList=cpList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(CompanyPosition::getZoneId))),ArrayList::new));

通过上面的这行代码可筛选CompanyPosition这个实体中zoneId的记录只留下一条记录,从而达到去重的效果。聪明的你学会了吗?

以下是Java中使用lambda表达式和Stream API对List对象根据属性进行的方法: 1. 使用lambda表达式和HashSet进行: ```java List<Person> list = new ArrayList<>(); // 假设Person类有个name属性 List<Person> distinctList = list.stream() .filter(person -> person.getName() != null) .collect(Collectors.toCollection(() -> new HashSet<>(person -> person.getName()))); ``` 2. 使用lambda表达式和LinkedHashSet进行,保持原有顺序: ```java List<Person> list = new ArrayList<>(); // 假设Person类有个name属性 List<Person> distinctList = list.stream() .filter(person -> person.getName() != null) .collect(Collectors.toCollection(() -> new LinkedHashSet<>(person -> person.getName()))); ``` 3. 使用Stream API的distinct方法进行: ```java List<Person> list = new ArrayList<>(); // 假设Person类有个name属性 List<Person> distinctList = list.stream() .filter(person -> person.getName() != null) .distinct() .collect(Collectors.toList()); ``` 4. 使用Stream API的collect方法和Collectors.groupingBy进行: ```java List<Person> list = new ArrayList<>(); // 假设Person类有个name属性 List<Person> distinctList = list.stream() .filter(person -> person.getName() != null) .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(person -> person.getName())), ArrayList::new)); ``` 5. 使用Stream API的collect方法和Collectors.toMap进行: ```java List<Person> list = new ArrayList<>(); // 假设Person类有个name属性 List<Person> distinctList = list.stream() .filter(person -> person.getName() != null) .collect(Collectors.collectingAndThen( Collectors.toMap(Person::getName, Function.identity(), (existing, replacement) -> existing), map -> new ArrayList<>(map.values()))); ``` 6. 使用Stream API的collect方法和Collectors.toMap进行,保持原有顺序: ```java List<Person> list = new ArrayList<>(); // 假设Person类有个name属性 List<Person> distinctList = list.stream() .filter(person -> person.getName() != null) .collect(Collectors.collectingAndThen( Collectors.toMap(Person::getName, Function.identity(), (existing, replacement) -> existing, LinkedHashMap::new), map -> new ArrayList<>(map.values()))); ``` 7. 使用Stream API的collect方法和Collectors.collectingAndThen进行: ```java List<Person> list = new ArrayList<>(); // 假设Person类有个name属性 List<Person> distinctList = list.stream() .filter(person -> person.getName() != null) .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new)); ``` 8. 使用Stream API的collect方法和Collectors.collectingAndThen进行,保持原有顺序: ```java List<Person> list = new ArrayList<>(); // 假设Person类有个name属性 List<Person> distinctList = list.stream() .filter(person -> person.getName() != null) .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new)); ```
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值