本文讲解如何利用 java stream api 正确、函数式地遍历 `list
在 Java 8+ 开发中,使用 Stream API 替代传统 for 循环处理集合是推荐实践,但需严格遵循无副作用(side-effect-free) 原则:即不应在 map、filter 等中间操作中修改外部状态(如向外部 HashMap 调用 put())。您原始代码中在 map() 内直接调用 responseMap.put(...) 虽能编译通过,但属于不安全的可变操作——不仅违背函数式编程思想,更可能在并行流(parallelStream())下引发竞态条件、数据丢失或 ConcurrentModificationException。
✅ 正确做法是:将每项请求转换为键值对(如 Map.Entry
MapresponseMap = requestList.stream() .map(requestedItem -> { String productId = requestedItem.getProductId(); Integer dbQuantity = productInDbMap.get(productId); // 注意空值安全 // 处理 productId 不在库存 Map 中的情况(建议防御性编程) if (dbQuantity == null) { return new AbstractMap.SimpleEntry<>(productId, "product-not-found"); } int requestedQty = requestedItem.getRequestedQuantity(); if (dbQuantity >= requestedQty) { return new AbstractMap.SimpleEntry<>(productId, "order-able"); } else { int shortfall = requestedQty - dbQuantity; // 注意:此处应为“缺货量”,非“剩余量” return new AbstractMap.SimpleEntry<>(productId, String.valueOf(shortfall)); } }) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (existing, replacement) -> existing // 解决 key 冲突(此处理论上不会发生,因 productId 唯一) ));
? 关键要点说明:
g 确保当意外出现重复 productId 时保留首个结果(也可根据需求抛出异常); ? 进阶建议:
遵循此模式,您将写出更清晰、更安全、更易测试和维护的 Java 函数式代码。