如何用Lambda从List<BusinessLog>中求和并获取最早日期
当然可以用Lambda+Stream API来实现,而且比你当前forEach的写法更优雅简洁。先帮你分析下现有代码的问题,再给出几种可行的实现方案:
问题分析与解决方案
首先看你定义的BusinessLog类:
public class BusinessLog { private Date logDate; private double prize; // 假设已提供getter方法:getLogDate()、getPrize() }
你当前用forEach的写法有个核心问题:BigDecimal是不可变类,balance.add(...)会返回一个新的BigDecimal对象,但你没有把这个新对象赋值回去,所以最终balance始终是初始的ZERO。而且forEach更适合单纯遍历执行操作,要同时完成累加和找最小值,用Stream的归约操作会更高效规范。
方式一:一次遍历完成两个操作(高效)
我们可以用一个容器类(比如JDK自带的SimpleEntry)同时存储总和与最早日期,通过reduce方法一次遍历完成计算,避免多次遍历列表:
if (list == null || list.isEmpty()) { // 处理空列表的默认逻辑,比如总和返回0,日期返回null BigDecimal totalPrize = BigDecimal.ZERO; Date earliestDate = null; return; } // 用SimpleEntry存储结果:key=prize总和,value=最早logDate AbstractMap.SimpleEntry<BigDecimal, Date> result = list.stream() .reduce( // 初始值:总和为0,日期先设为第一个元素的日期 new AbstractMap.SimpleEntry<>(BigDecimal.ZERO, list.get(0).getLogDate()), // 迭代逻辑:累加prize并更新最早日期 (currentEntry, log) -> { BigDecimal newSum = currentEntry.getKey().add(BigDecimal.valueOf(log.getPrize())); Date newMinDate = currentEntry.getValue().before(log.getLogDate()) ? currentEntry.getValue() : log.getLogDate(); return new AbstractMap.SimpleEntry<>(newSum, newMinDate); }, // 并行流场景下的合并逻辑(串行流可忽略,但加上更规范) (entry1, entry2) -> { BigDecimal totalSum = entry1.getKey().add(entry2.getKey()); Date totalMinDate = entry1.getValue().before(entry2.getValue()) ? entry1.getValue() : entry2.getValue(); return new AbstractMap.SimpleEntry<>(totalSum, totalMinDate); } ); // 提取结果 BigDecimal totalPrize = result.getKey(); Date earliestDate = result.getValue();
方式二:分开用Stream的sum和min操作(简洁易读)
如果列表数据量不大,分开执行两个Stream操作会让代码更简洁,可读性更高:
if (list == null || list.isEmpty()) { BigDecimal totalPrize = BigDecimal.ZERO; Date earliestDate = null; return; } // 计算所有prize的总和 BigDecimal totalPrize = list.stream() .map(log -> BigDecimal.valueOf(log.getPrize())) .reduce(BigDecimal.ZERO, BigDecimal::add); // 获取最早的logDate Date earliestDate = list.stream() .map(BusinessLog::getLogDate) .min(Date::compareTo) .orElse(null); // 列表为空时返回null
再说说你当前forEach写法的问题
你的代码里:
BigDecimal balance = BigDecimal.ZERO; if (list != null) { list.forEach(businessLog -> { balance.add(BigDecimal.valueOf(businessLog.getPrize())); // 无效操作 }); }
除了BigDecimal不可变的问题外,Lambda表达式中引用的外部变量必须是final或有效final,所以你也不能直接在Lambda里给balance赋值(比如balance = balance.add(...)会直接编译报错)。这也是为什么处理这类聚合场景,Stream的归约操作是更合适的选择。
内容的提问来源于stack exchange,提问作者tyro
相关产品推荐
相关产品推荐

