Java 8流是安全返回类型吗?

时间:2022-01-06 19:06:36

Are Java 8 Streams safe return types for public methods, in that it would be impossible to mutate the underlying object given a stream from it?

对于公共方法来说,Java 8流是安全的返回类型吗?因为给定一个流,它不可能对底层对象进行修改。

For example, if I have a List and return list.stream(); could the return value in any way be used to mutate the original list?

例如,如果我有一个列表并返回List .stream();返回值是否可以用于改变原始列表?

Judging from the API, I don't think it's possible but would like to confirm.

从API来看,我认为这是不可能的,但我想确认一下。

1 个解决方案

#1


13  

Yes, it is safe to do so. Streams do not/should not modify the underlying data structure.

是的,这样做是安全的。流不/不应该修改底层数据结构。

A few excerpts from java.util.stream.Stream:

以下是来自java.util.stream.Stream的一些摘录:

A sequence of elements […].

元素的序列[…]。

Collections and streams, while bearing some superficial similarities, have different goals. Collections are primarily concerned with the efficient management of, and access to, their elements. By contrast, streams do not provide a means to directly access or manipulate their elements […].

收集和流,虽然有一些表面的相似性,但有不同的目标。集合主要关注对其元素的有效管理和访问。相比之下,流不提供直接访问或操作其元素的方法[…]。

To preserve correct behavior, [behavioral parameters to stream operations …] must be non-interfering (they do not modify the stream source).

为了保持正确的行为,[流操作的行为参数…]必须是非干扰性的(它们不修改流源)。

And from Package java.util.stream Description:

java.util和包。流程描述:

Streams differ from collections in several ways:

流与集合的不同有以下几个方面:

  • No storage. A stream is not a data structure that stores elements; instead, it conveys elements from a source […], through a pipeline of computational operations.
  • 没有存储。流不是存储元素的数据结构;相反,它通过计算操作管道从源[…]传递元素。
  • Functional in nature. An operation on a stream produces a result, but does not modify its source.
  • 功能在本质上。流上的操作产生结果,但不修改其源。

You might also see Non-interference.

您可能还会看到无干扰。


[…] it would be impossible to mutate the underlying object given a stream from it.

如果给定一个流,就不可能改变底层对象。

While it would be possible to write our own implementation of java.util.Stream that modified the underlying data structure, it would be an error to do so. ; )

虽然可以编写自己的java.util实现。如果流修改了底层数据结构,那么这样做将是错误的。,)


In response to the comment by @AlexisC.:

回应@AlexisC的评论。

Getting a stream from the list […] can modify its content if it contains mutable objects.

从列表[…]获取流可以修改它的内容,如果它包含可变对象。

This is a fair point. If we have a stream of elements which are mutable, we can do:

这是一个公平的观点。如果我们有一个可变的元素流,我们可以:

myObj.stream().forEach(( Foo foo ) -> ( foo.bar = baz ));

#1


13  

Yes, it is safe to do so. Streams do not/should not modify the underlying data structure.

是的,这样做是安全的。流不/不应该修改底层数据结构。

A few excerpts from java.util.stream.Stream:

以下是来自java.util.stream.Stream的一些摘录:

A sequence of elements […].

元素的序列[…]。

Collections and streams, while bearing some superficial similarities, have different goals. Collections are primarily concerned with the efficient management of, and access to, their elements. By contrast, streams do not provide a means to directly access or manipulate their elements […].

收集和流,虽然有一些表面的相似性,但有不同的目标。集合主要关注对其元素的有效管理和访问。相比之下,流不提供直接访问或操作其元素的方法[…]。

To preserve correct behavior, [behavioral parameters to stream operations …] must be non-interfering (they do not modify the stream source).

为了保持正确的行为,[流操作的行为参数…]必须是非干扰性的(它们不修改流源)。

And from Package java.util.stream Description:

java.util和包。流程描述:

Streams differ from collections in several ways:

流与集合的不同有以下几个方面:

  • No storage. A stream is not a data structure that stores elements; instead, it conveys elements from a source […], through a pipeline of computational operations.
  • 没有存储。流不是存储元素的数据结构;相反,它通过计算操作管道从源[…]传递元素。
  • Functional in nature. An operation on a stream produces a result, but does not modify its source.
  • 功能在本质上。流上的操作产生结果,但不修改其源。

You might also see Non-interference.

您可能还会看到无干扰。


[…] it would be impossible to mutate the underlying object given a stream from it.

如果给定一个流,就不可能改变底层对象。

While it would be possible to write our own implementation of java.util.Stream that modified the underlying data structure, it would be an error to do so. ; )

虽然可以编写自己的java.util实现。如果流修改了底层数据结构,那么这样做将是错误的。,)


In response to the comment by @AlexisC.:

回应@AlexisC的评论。

Getting a stream from the list […] can modify its content if it contains mutable objects.

从列表[…]获取流可以修改它的内容,如果它包含可变对象。

This is a fair point. If we have a stream of elements which are mutable, we can do:

这是一个公平的观点。如果我们有一个可变的元素流,我们可以:

myObj.stream().forEach(( Foo foo ) -> ( foo.bar = baz ));