Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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?

like image 281
Tea Avatar asked Sep 11 '16 03:09

Tea


People also ask

What is the primary difference between a collection and a stream?

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.

Do Java streams need to be closed?

Streams have a BaseStream. close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files. lines(Path, Charset)) will require closing.

What is the difference between collections for each and streams for each?

forEach takes the collection's lock once and holds it across all the calls to the action method. The Stream. forEach call uses the collection's spliterator, which does not lock, and which relies on the prevailing rule of non-interference.

Why is collection and stream different?

Collections are used to store and group the data in a particular data structure like List, Set or Map. But, streams are used to perform complex data processing operations like filtering, matching, mapping etc on stored data such as arrays, collections or I/O resources.


1 Answers

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

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 Stream obtained from a collection produces a new Stream without 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 String with 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.

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

like image 72
Andreas Avatar answered Sep 19 '22 19:09

Andreas