忘记与第一个代码段相关。我根本不会用
forEach。由于您将的元素收集
Stream到中
List,因此以结束
Stream处理会更有意义
collect。然后,您将需要
peek设置ID。
List<Entry> updatedEntries = entryList.stream() .peek(e -> e.setTempId(tempId)) .collect (Collectors.toList());
对于第二个代码段,
forEach可以执行多个表达式,就像任何lambda表达式都可以:
entryList.forEach(entry -> { if(entry.getA() == null){ printA(); } if(entry.getB() == null){ printB(); } if(entry.getC() == null){ printC(); }});但是,(请看您的评论尝试),在这种情况下您不能使用过滤器,因为
entry.getA() ==null如果您这样做,它只会处理某些条目(例如,针对的条目)。



