Java多字段作为汇总条件,进行汇总运算

 前言

工作过程中遇到需要将导入的三张excel表进行运算,然后导出一张结果的excel表,所以未涉及到SQL语句
最开始这块代码是用于汇总求和,然后变成了记录最后一行的值便可以了。

实现代码

// 创建一个Map来存储分组后的结果
            Map<String, LackBom> groupedBomMap = CombinateBom_Warn.stream()
                    .collect(Collectors.groupingBy(
                            bom -> bom.getComponent1() + "_" + bom.getComponent2() + "_" + bom.getComponent3() + "_" + bom.getComponent_Description() + "_" + bom.getUsage() + "_" + bom.getMRP(),
                            Collector.of(
                                    LackBom::new, // 供应商函数,用于创建一个新的LackBom对象作为累加器
                                    (accumulator, bom) -> {
                                        accumulator.setInventoryAccount(bom.getInventoryAccount());
                                        // 如果需要,可以设置其他字段为第一个bom的对应值
                                        if (accumulator.getComponent1() == null) {
                                            accumulator.setComponent1(bom.getComponent1());
                                        }
                                        if(accumulator.getComponent2() == null){
                                            accumulator.setComponent2(bom.getComponent2());
                                        }
                                        if(accumulator.getComponent3() == null){
                                            accumulator.setComponent3(bom.getComponent3());
                                        }
                                        if(accumulator.getComponent_Description() == null){
                                            accumulator.setComponent_Description(bom.getComponent_Description());
                                        }
                                        if(accumulator.getMRP() == null){
                                            accumulator.setMRP(bom.getMRP());
                                        }
                                    },
                                    (left, right) -> {
                                        // 合并函数,用于合并两个累加器的状态
                                        left.setInventoryAccount(right.getInventoryAccount());
                                        // 如果需要,可以在这里合并其他字段(但在这个例子中它们应该是相同的)
                                        return left;
                                    }
                            )
                    ));

            // 将Map的值转换为List
            List<LackBom> CombinateBom_Warns = new ArrayList<>(groupedBomMap.values());

 最后
 

因为我主要学的C#,对Java这边只停留在算法题,所以很希望有朋友分享一下更方便的方法。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小先生812

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值