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

前端 未结 5 1763
佛祖请我去吃肉
佛祖请我去吃肉 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:55

    You can do it using streams map function like below, get result in new stream for further processing.

    Stream newFruits = fruits.stream().map(fruit -> {fruit.name+="s"; return fruit;});
            newFruits.forEach(fruit->{
                System.out.println(fruit.name);
            });
    

提交回复
热议问题