我想更改 feeAmount 和消息,使用 for 循环我做了,但我想使用 Lambda(默认情况下所有变量在 Lambda 中都是最终的)。
gridFeeEntity.stream().forEach(entity -> {
if ("T".equals(entity.getType())){
feeAmount += 1;
}else if ("P".equals(entity.getType())) {
feeAmount += 2;
} else {
message = "not Supported";
}
});
Run Code Online (Sandbox Code Playgroud)
除了蒂姆的回答,我建议考虑反转你的逻辑。(请注意,您的“消息”位几乎肯定是一个例外。)
int feeIncrease = gridFeeEntities.stream()
.map(GridFeeEntity::getType)
.mapToInt(FeeList::forType)
.sum();
feeAmount += feeIncrease;
Run Code Online (Sandbox Code Playgroud)