Set attribute in list of objects in Java using Lambda

I have a list of objects and I want to populate one attribute for each object in the list. I can do this using a loop for this.

for(Car car : cars) {
    String abcd = getSomeValue();
    car.setAbcd(abcd);
}

Can I do the same using Lambda in Java8.

+4
source share
1 answer

Yes you can use forEach:

cars.forEach(c -> c.setAbcd(getSomeValue()));
+8
source

Source: https://habr.com/ru/post/1612206/


All Articles