Tag Archives: Java 5

Java 8 Stream API Tutorial

Java 8 Stream API Tutorial

Java 8 offers several new functionalities. One of the most important is the new Streams API. Basically, Streams are sequences of elements that support concatenated operations. They used a source and allow different intermediate and terminal operations. The combination of source and all the operations involved is called stream pipeline (because Streams allow operations to be pipelined or concatenated).

As source we can have collections, arrays, lines of a file, files in a directory or numeric ranges; Intermediate operations are filter, map, distict or flatMap; several intermediate operations can be concatenated. Terminal operations are for example forEach, collect, reduce and min or max. Only one terminal operation is possible, if a terminal operation is executed the stream will be closed and cannot be used again.

Streams and Lambdas work very well together, make the code more clear and concise and offer several possibilities like parallelism. As information, the Streams API has been implemented using Lambdas as well.

In this article we are going to show how to create Streams from different sources and how to use the main Streams operations.

All examples are being implemented using Eclipse Luna version 4.4 and Java version 8 update 5.