Java8集合中的对象根据指定字段去重,并根据条件获取指定去重后的对象

Java8集合中的对象根据指定字段去重,并根据条件获取指定去重后的对象

一、distinct去重

 List uniqueList = list.stream().distinct().collect(Collectors.toList());

二、collectingAndThen去重

一个字段:

ArrayList<PaperRecord> paperRecordList = list.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(PaperRecord::getUserId))), ArrayList::new)
            );

多个字段:

ArrayList<PaperRecord> paperRecordList = list.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(item->item.getUserId()+"_"+item.getPassStatus()))), ArrayList::new)
            );

三、Collectors.toMap去重

去重取旧对象:

List<PaperRecord> paperRecordList = new ArrayList<>(list.stream()
                    .collect(Collectors.toMap(item -> item.getUserId(), Function.identity(), (oldValue, newValue) -> oldValue))
                    .values());

去重根据条件取对象:

List<PaperRecord> paperRecordList = new ArrayList<>(list.stream()
                    .collect(Collectors.toMap(item -> item.getUserId(), Function.identity(), (oldValue, newValue) -> oldValue.getPassStatus() ? oldValue : newValue))
                    .values());
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java集合根据指定字段去重有多种方法可供选择。其一种常见的方法是使用Stream API和Collectors工具类进行操作。引用介绍了8种不同的方法,以下是其一种方法的示例代码: ``` List<TalentPlanStudentEntity> studentList = relatePlanStudentList.stream() .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TalentPlanStudentEntity::getUserId))), ArrayList::new)); ``` 这段代码使用了stream()方法将集合转换为流,然后使用collect()方法结合Collectors.collectingAndThen()方法和Collectors.toCollection()方法进行操作。具体来说,Collectors.toCollection()方法创建了一个TreeSet集合,并通过Comparator.comparing()方法指定了根据哪个字段进行比较。最后,通过ArrayList::new将结果转换为ArrayList类型的集合。这样就可以实现根据指定字段去重的效果。引用的代码示例展示了Collectors.toMap()方法的使用,可以根据自己的需求来选择适合的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JavaList集合对象去重及按属性去重的8种方法](https://download.csdn.net/download/weixin_38624556/12822011)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [java8 List根据某个字段去重](https://blog.csdn.net/xiaojuge/article/details/124377644)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值