Modify property value of the objects in list using Java 8 streams

前端 未结 5 1773
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 04:26

I have a list of Fruit objects in ArrayList and I want to modify fruitName to its plural name.

Refer the example:

@Data
@Al         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 04:58

    You can use peek to do that.

    List newList = fruits.stream()
        .peek(f -> f.setName(f.getName() + "s"))
        .collect(Collectors.toList());
    

提交回复
热议问题