Java 8 中的流和集合有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39432699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:22:37  来源:igfitidea点击:

What is the difference between Streams and Collections in Java 8

javacollectionsjava-8java-stream

提问by Tea

I'm learning about Streams in Java 8. I got confused about this concept:

我正在学习 Java 8 中的 Streams。我对这个概念感到困惑:

A collection is an in-memory data structure, which holds all the values that the data structure currently has—every element in the collection has to be computed before it can be added to the collection. In contrast, a stream is a conceptually fixed data structure in which elements are computed on demand.

集合是一种内存中的数据结构,它保存数据结构当前拥有的所有值——集合中的每个元素都必须在将其添加到集合之前进行计算。相比之下,流是一种概念上固定的数据结构,其中元素是按需计算的。

I don't understand. How can a Collection only hold values that must have been computed before they can be added to the collection? And also, what is meant by the comparison of a Stream with a fixed data structure?

我不明白。一个 Collection 怎么能只保存那些在添加到集合之前必须计算过的值?而且,将 Stream 与固定数据结构进行比较是什么意思?

回答by Andreas

You didn't provide the source of your quote, so let me quote the javadocto you:

您没有提供报价的来源,所以让我向您引用javadoc

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 such as a data structure, an array, a generator function, or an I/O channel, through a pipeline of computational operations.
  • Functional in nature. An operation on a stream produces a result, but does not modify its source. For example, filtering a Streamobtained from a collection produces a new Streamwithout the filtered elements, rather than removing elements from the source collection.
  • Laziness-seeking. Many stream operations, such as filtering, mapping, or duplicate removal, can be implemented lazily, exposing opportunities for optimization. For example, "find the first Stringwith three consecutive vowels" need not examine all the input strings. Stream operations are divided into intermediate (Stream-producing) operations and terminal (value- or side-effect-producing) operations. Intermediate operations are always lazy.
  • Possibly unbounded. While collections have a finite size, streams need not. Short-circuiting operations such as limit(n)or findFirst()can allow computations on infinite streams to complete in finite time.
  • Consumable. The elements of a stream are only visited once during the life of a stream. Like an Iterator, a new stream must be generated to revisit the same elements of the source.

流在几个方面与集合不同:

  • 没有存储。流不是存储元素的数据结构;相反,它通过计算操作的管道传送来自数据结构、数组、生成器函数或 I/O 通道等源的元素。
  • 功能性。对流的操作会产生结果,但不会修改其源。例如,过滤Stream从集合中获得的 a 会产生一个Stream没有过滤元素的新元素,而不是从源集合中删除元素。
  • 懒惰寻求。许多流操作,例如过滤、映射或重复删除,可以懒惰地实现,从而暴露优化机会。例如,“找到String具有三个连续元音的第一个”不需要检查所有输入字符串。流操作分为中间(Stream生产)操作和终端(价值或副作用生产)操作。中间操作总是懒惰的。
  • 可能无界。虽然集合具有有限的大小,但流不需要。诸如limit(n)或 之类的短路操作findFirst()可以允许在有限时间内完成对无限流的计算。
  • 消耗品。流的元素在流的生命周期内仅被访问一次。与 一样Iterator,必须生成一个新流以重新访问源的相同元素。

In contrast, a Collectionis a containerof objects (elements). You can't get (retrieve) an object from a collection unless the object was previously added to the collection.

相反, aCollection是对象(元素)的容器。您无法从集合中获取(检索)对象,除非该对象之前已添加到集合中。