java根据类的一个属性对List去重

java根据类的一个属性对List去重

    public Page<UserActivityRegVO> regRecord(RecordQueryParam recordQueryParam, PaobaPage paobaPage) {
        if(recordQueryParam.getCardId() == null && recordQueryParam.getOpenId() == null){
            throw new PbServiceException("请登录小程序后查询个人报名记录!");
        }else {
            Page<UserActivityRegVO> map = playerRepository.regPage(recordQueryParam, paobaPage).map(
                    tuple2 -> {
                        tuple2._1();
                        ActivityVO activityVO = activityAssembler.E2V(tuple2._2());
                        UserActivityRegVO activityRegVO = new UserActivityRegVO();
                        BeanUtil.copyProperties(activityVO, activityRegVO);
                        activityRegVO.setUserRegTime(tuple2._1().getCreateTime());
                        return activityRegVO;
                    }
            );
            List<UserActivityRegVO> content = map.getContent();
            List<UserActivityRegVO> res = content.stream().collect(Collectors.collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserActivityRegVO::getActId))), ArrayList::new
            ));

            return new PageImpl<>(res,PageRequest.of(paobaPage.getPn(), paobaPage.getPs()),map.getTotalElements());
        }
    }

使用流操作去重:

content.stream().collect(Collectors.collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserActivityRegVO::getActId))), ArrayList::new
            ));

记录一下。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 要根据一个list属性去重一个list,你可以使用Java 8的Stream API来实现。其中,你可以使用filter方法和distinctByProperty方法来过滤和去重list。以下是一个示例代码: List<MyObject> myList = ... // 你的对象列表 List<MyObject> distinctList = myList.stream().filter(distinctByProperty(MyObject::getSomeProperty)).collect(Collectors.toList()); 其中,distinctByProperty是一个自定义的方法,用于根据对象的某个属性进行去重。你可以根据你的需求来实现这个方法。\[2\] 另外,你还可以使用collectingAndThen方法和toCollection方法来实现去重。以下是一个示例代码: List<Person> people = new ArrayList<>(); Person p111 = new Person(); p111.setId(111); p111.setName("Yang"); p111.setAge(31); people.add(p111); Person p112 = new Person(); p112.setId(111); p112.setName("Yang"); p112.setAge(31); people.add(p112); Person p113 = new Person(); p113.setId(112); p113.setName("Liu"); p113.setAge(22); people.add(p113); people = people.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getId))), ArrayList::new)); 在这个示例中,我们使用了collectingAndThen方法和toCollection方法来创建一个TreeSet,并根据Person对象的id属性进行去重。最后,我们将去重后的结果转换为ArrayList。\[3\] 希望这些示例代码能够帮助到你! #### 引用[.reference_title] - *1* *2* [java List对象> 根据对象的一个属性进行去重](https://blog.csdn.net/sunyanhu_1/article/details/131469357)[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^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [java8 List根据元素对象属性去重](https://blog.csdn.net/u014082714/article/details/128144539)[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^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值