Java 8 Stream Tutorial


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Java 8 Stream Tutorial

Stream operations

  • intermediate
    • Return: a stream
    • Ex: filter sorted map
  • terminal
    • Return: void or non-stream result
    • Ex: forEach


Processing Order

This doesn't have terminal operation so no output will be displayed. That is because intermediate operations will only be executed when a terminal operation is present.

The above example have teminal operation

To avoid uncessary calls, instead of executing the operations horizontally we can do it vertically:

The operation anyMatch returns true as soon as the predicate applies to the given input element. This is true for the second element passed "A2". Due to the vertical execution of the stream chain, map has only to be executed twice in this case. So instead of mapping all elements of the stream, map will be called as few as possible.

Reusing Streams

Java 8 streams cannot be reused. As soon as you call any terminal operation the stream is closed: Calling noneMatch after anyMatch on the same stream results in Exeption.

To overcome this limitation we have to to create a new stream chain for every terminal operation we want to execute, e.g. we could create a stream supplier to construct a new stream with all intermediate operations already set up:

Each call to get() constructs a new stream on which we are save to call the desired terminal operation.




Parallel Streams

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content