Streams

Learn about streams in Java 8.

Stream interface

The Stream interface is located in the java.util.stream package. It represents a sequence of objects somewhat like the Iterator interface. However, unlike the Iterator, it supports parallel execution.

The Stream interface supports the map/filter/reduce pattern and executes lazily, forming the basis (along with lambdas) for functional-style programming in Java 8.

There are also corresponding primitive streams, IntStream, DoubleStream, and LongStream, for performance reasons.

Generating streams

The most obvious way to create a stream is from a Collection. The Collection interface has two default methods for creating streams:

  • stream(): Returns a sequential Stream with the collection as its source.
  • parallelStream(): Returns a possibly parallel Stream with the collection as its source.

The ordering of the Stream relies on the underlying collection just like an Iterator.

Streaming files

The BufferedReader now has the ...