Lambda实现两个List根据某个字段比较增量、减量、同量方法

该代码示例展示了如何使用Java Stream处理两个客户信息列表,分别找出减量(在保-非在保)、增量(新增用户)和同量(更新用户)的数据。通过对oldInfoList和latestInfoList进行过滤操作,实现数据对比并打印结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 public static void main(String[] args) {
        List<TestBody> oldInfoList = new ArrayList<>();
        TestBody customer1 = new TestBody();
        customer1.setId("old1");
        customer1.setCustomerId("new111111");
        oldInfoList.add(customer1);

        TestBody customer2 = new TestBody();
        customer2.setId("old2");
        customer2.setCustomerId("new222222");
        oldInfoList.add(customer2);

        TestBody customer3 = new TestBody();
        customer3.setId("old3");
        customer3.setCustomerId("old888888");
        oldInfoList.add(customer3);

        TestBody customer4 = new TestBody();
        customer4.setId("old4");
        customer4.setCustomerId("old999999");
        oldInfoList.add(customer4);


        List<TestBody> latestInfoList = new ArrayList<>();
        TestBody customerInfoDO1 = new TestBody();
        customerInfoDO1.setId("new1");
        customerInfoDO1.setCustomerId("new111111");
        latestInfoList.add(customerInfoDO1);

        TestBody customerInfoDO2 = new TestBody();
        customerInfoDO2.setId("new2");
        customerInfoDO2.setCustomerId("new222222");
        latestInfoList.add(customerInfoDO2);

        TestBody customerInfoDO3 = new TestBody();
        customerInfoDO3.setId("new3");
        customerInfoDO3.setCustomerId("new333333");
        latestInfoList.add(customerInfoDO3);

        TestBody customerInfoDO4 = new TestBody();
        customerInfoDO4.setId("new4");
        customerInfoDO4.setCustomerId("new444444");
        latestInfoList.add(customerInfoDO4);


        System.out.println("老数据:"+ oldInfoList);
        System.out.println("新数据:"+ latestInfoList);

        // 4.1、减量:oldSyncSmsInfoList(customerType:在保->非在保(["type1"]->["type5"]))
        List<TestBody> reduceList = oldInfoList.stream().filter(x -> !latestInfoList.stream().map(TestBody::getCustomerId).collect(Collectors.toList())
                .contains(x.getCustomerId())).collect(Collectors.toList());
        if (!ObjectUtils.isEmpty(reduceList)) {
            log.info("减量用户" + reduceList);
        }

        // 4.2、增量:(新增用户)
        List<TestBody> addList = latestInfoList.stream().filter(x -> !oldInfoList.stream().map(TestBody::getCustomerId)
                .collect(Collectors.toList()).contains(x.getCustomerId())).collect(Collectors.toList());
        if (!ObjectUtils.isEmpty(addList)) {
            log.info("新增用户:" + addList);
        }

        // 4.3、同量:更新用户数据,抓取最新的更新
        List<TestBody> sameList = latestInfoList.stream().filter(x -> oldInfoList.stream().map(TestBody::getCustomerId).collect(Collectors.toList())
                .contains(x.getCustomerId())).collect(Collectors.toList());
        if (!ObjectUtils.isEmpty(sameList)) {
            log.info("同量用户信息:" + sameList);
        }

    }

 实现效果如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值