迭代时在Java8中修改流中的对象

2022-01-22 00:00:00 java-8 java java-stream

在 Java8 流中,我可以修改/更新其中的对象吗?例如.列表<用户>用户:

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

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

推荐答案

是的,您可以修改流中对象的状态,但通常您应该避免修改 source 的流.来自 无干扰 流包文档的部分我们可以阅读:

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:

对于大多数数据源,防止干扰意味着确保在流管道执行期间根本不修改数据源.值得注意的例外是其源是并发集合的流,这些集合专门设计用于处理并发修改.并发流源是那些 Spliterator 报告 CONCURRENT 特征的源.

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.

这样就好了

  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));
//^^^^^                       ^^^^^^^^^^^^
//     \_____________________/

并可能抛出 ConcurrentModificationException 甚至其他意外异常,例如 NPE:

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

相关文章