Is it possible to make changes to all objects in a collection at once?
e.g. (this is impossible):
thisCollection.getAll().setTested(true);
There is no getAll
and so I wondered if there was another way to make this change while avoiding iteration.
Well you can use Iterable.forEach
:
thisCollection.forEach(x -> x.setTested(true));
That's still going to iterate, of course - there's nothing you can do about that. You've got lots of objects to modify... how would you expect them to all be modified without any iteration?
It's up to you whether you view the above as simpler or more complicated than just doing it the "old fashioned" way with an enhanced for loop:
for (Item item : thisCollection) {
item.setTested(true);
}
See more on this question at Stackoverflow