前言
工作过程中遇到需要将导入的三张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这边只停留在算法题,所以很希望有朋友分享一下更方便的方法。