Java – Modifying Objects within stream in Java8 while iterating

java, java-8, java-stream

In Java8 streams, am I allowed to modify/update objects within?
For eg. List<User> users:

users.stream().forEach(u -> u.setProperty("value"))

Best Solution

Yes, you can modify state of objects inside your stream, but most often you should avoid modifying state of source of stream. From non-interference section of stream package documentation we can read that:

For most data sources, preventing interference means ensuring that the data source is not modified at all during the execution of the stream pipeline. The notable exception to this are streams whose sources are concurrent collections, which are specifically designed to handle concurrent modification. Concurrent stream sources are those whose Spliterator reports the CONCURRENT characteristic.

So this is OK

  List<User> users = getUsers();  users.stream().forEach(u -> u.setProperty(value));//                       ^    ^^^^^^^^^^^^^//                        \__/

but this in most cases is not

  users.stream().forEach(u -> users.remove(u));//^^^^^                       ^^^^^^^^^^^^//     \_____________________/

and may throw ConcurrentModificationException or even other unexpected exceptions like NPE:

List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());list.stream()    .filter(i -> i > 5)    .forEach(i -> list.remove(i));  //throws NullPointerException