简体   繁体   English

Java 8中的Streams和Collections有什么区别

[英]What is the difference between Streams and Collections in Java 8

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.相比之下,stream 是概念上固定的数据结构,其中的元素是按需计算的。

I don't understand, how can a Collection only hold values that must have been computed before they can be added to the collection?我不明白,Collection 怎么能只保存必须在添加到集合之前必须计算的值? And also, what is meant by the comparison of a Stream with a fixed data structure?还有,将 Stream 与固定数据结构进行比较是什么意思?

You didn't provide the source of your quote, so let me quote the javadoc to 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.相反,它通过计算操作的管道传送来自数据结构、数组、生成器函数或 I/O 通道等源的元素。
  • Functional in nature .功能性 An operation on a stream produces a result, but does not modify its source.对流的操作会产生结果,但不会修改其源。 For example, filtering a Stream obtained from a collection produces a new Stream without the filtered elements, rather than removing elements from the source collection.例如,过滤从集合中获取的Stream会生成一个没有过滤元素的新Stream ,而不是从源集合中删除元素。
  • 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 String with three consecutive vowels" need not examine all the input strings.例如,“找到具有三个连续元音的第一个String ”不需要检查所有输入字符串。 Stream operations are divided into intermediate ( Stream -producing) operations and terminal (value- or side-effect-producing) operations.流操作分为中间( Stream生产)操作和终端(价值或副作用产生)操作。 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. limit(n)findFirst()等短路操作可以允许在有限的时间内完成对无限流的计算。
  • 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.Iterator一样,必须生成一个新流以重新访问源的相同元素。

In contrast, a Collection is a container of objects (elements).相反, Collection是对象(元素)的容器 You can't get (retrieve) an object from a collection unless the object was previously added to the collection.除非先前已将对象添加到集合中,否则您无法从集合中获取(检索)对象。

Some basic differences are一些基本的区别是

Collection 和 Stream 的区别

But these are some finite differences, you have to explore to know more.但这些都是一些有限的差异,你必须探索才能了解更多。 Collections are like CD/DVD and stream's are the movies. Collections 就像 CD/DVD 和流的是电影。 Streams can be stateless and statefull.流可以是无状态的和有状态的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM