Streams Features
Description
Java Streams have its own features.
No Storage
Java Streams have no storage.
A collection is an in-memory data structure that stores all its elements.
A stream has no storage. A stream pulls elements from a data source on-demand and passes them to a pipeline of operations for processing.
For a collection we talk about the storage or how the data elements are stored, how to access data elements.
For a stream we focus on the operations, for example, how to sum a stream.
Infinite Streams
A collection cannot represent a group of infinite elements whereas a stream can.
A stream can pull its elements from a data source. The data source can be a collection, a function that generates data, an I/O channel, etc.
A stream can pull data from from a function which generates infinite number of elements.
Not Reusable
Streams Are Not Reusable
A stream cannot be reused after calling a terminal operation.
To perform a computation on the same elements from the same data source, we have to recreate the stream pipeline.
A stream may throw an IllegalStateException in case of reusing.