Java 8 Streams in a Nutshell

Lochana Ranaweera
Lochness Writes
Published in
3 min readNov 23, 2015

If you have done programming with java, I’m sure by now you must have realized the importance of the java collections framework (JCF). A collection is a data structure which contains and processes a set of data. Examples of collections data structures are lists, sets, queues etc. The Wiki definition identifies the collections framework as a set of classes and interfaces that implement these commonly reusable collections.

All this time, whenever a collection had to be processed, we used to iterate over the elements in the collection to do some work with each of them. As such, the specific implementation of how the iteration has to be carried and what has to be done with each element was the responsibility of the programmer. The drawback in this approach is, when it comes to large collections, the programmer will be burdened with the task coming up with traversal strategies which are efficient and concurrently executable. Coming up such parallel execution logic is difficult and error-prone.

Java SE 8 releases the programmers from the burden of having to incorporate parallelism and efficiency in their code through the all new Stream API! So how does streams do this?

~ “What, Not How” Principle:

Streams follow the “what, not how” principle. When using streams you have to specify “what” needs to be done with the contents of the stream, not “how” the process is carried out. The implementation of how the process is carried out is no longer a concern of the programmer.

Let’s see an example:

Suppose we want to print out all the long words in a text file. Notice the method initialize(). Here, I have first read the file contents into a String called fileContents. Then taking non letters as delimiters I have split the String into words using a regular expression and then created the list called words. In order to find the long words, I have iterated over the entire list and then printed the words in the method withoutStreams() as shown below.

Listing 1:

Output for Listing 1:

Now let’s try to do the same using Java 8 Stream API. The only difference in the code below from what we did earlier is the method withStreams(). In here, the stream method results in a stream for the list words. The filter method also returns a stream that contains only the words of size greater than twelve. The forEach method prints out each element of the stream returned by the filter method and terminate the stream operation.

Listing 2:

Output for Listing 2:

So you must be wondering what is so extraordinary in using Streams. The Oracle documentation for streams says, “Collections and streams while bearing some superficial similarities, have different goals”. A Stream, similar to a Collection allows you to transform and retrieve data. But following are the major differences between them;

  • Collections provide a means to directly access or manipulate their elements, while streams do not mutate the source, but return new streams that hold the result.
  • Streams carry out lazy operations. That means an operation is not carried out until the result is needed, therefore source elements are consumed only as needed.
    In the example I have given until the terminal operation forEach is being called the previous filter and stream operations are not performed.
  • To perform a computation, stream operations are composed into a stream pipeline. A stream pipeline consists of a source, zero or more intermediate operations and a terminal operation.
    Intermediate operations always return a new stream and are “lazy”. Also there are two types of intermediate operations; stateful and stateless. A normal stream filter like the one shown in Listing 2 is a stateless operation whereas a filter for finding distinct elements of a stream would be a stateful operation as previous results have to be remembered to carry it out. Once a terminal operation has been carried out, the stream will be terminated and it will not be possible to use it again.

I will discuss more on stream operations in the posts coming up!

--

--