Java Stream Interface

· Mik's TIL


Streams in Java are a way to lazily consume a possibly infinite amount of data in memory sequentially or, in some cases, even in parallel.

Streams are consumed in stream pipelines, which are composed by:

  1. a source;
  2. 0 or more intermediate operations;
  3. a terminal operation.

The source could be, to give a few examples, a Collection, an Array or even an I/O channel. These are typically consumed sequentially.

The intermediate operations, like filter() or .map(), always return a new stream and are lazy, that is, they are executed only when the terminal operation is invoked, so the traversal only happens at the end - not for each operation.

Terminal operations, such as forEach() or findFirst(), trigger the consume of the stream. These are optimized to execute the fewer possible operations, as an example, the findFirst() terminal will only process the first item in the stream.

Once the stream is consumed, it can't be used anymore, and a new one must be created starting from the source.

A few key notes: