How can I call the setter in chain of Stream without using forEach()?
List<Foo> newFoos = foos.stream()
.filter(foo -> Foo::isBlue)
.map(foo -> foo.setTitle("Some value")) //I am unable to use this because also changing the data type into Object
.collect(Collectors.toList());
forEach
seems like a more suited tool for the job, but if you don't want to use it you could always define an anonymous multi-line lambda:
List<Foo> foos = foos.stream()
.filter(foo -> Foo::isBlue)
.map(foo -> {
foo.setTitle("Some value");
return foo;
})
.collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With