Overview

The Stream API offers easy filtering, counting, and a mapping of collections, as well as different ways to get slices and subsets of information out of them.

It also allows shorter and more elegant code for working with collections.

What is a Stream?

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

There are many ways to create a Stream in Java 8. Many of the existing Java core library classes have Stream returning methods in Java 8.

  • Streaming Collections

    The most obvious way to create a stream is from a Collection.

    The Collection interface has two default methods on it for creating streams:

    1. stream(): Returns a sequential Stream with the collection as its source.
    2. 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 lines() method which returns a Stream; for example:

    You can also read a file as a Stream using Files.lines(Path filePath); for example:

    Note this populates lazily; it does not read the entire file when you call it.

  • Streaming File Trees

    There are several static methods on the Files class for navigating file trees using a Stream.

    1. list(Path dir): Stream of files in the given directory.
    2. walk(Path dir): Stream that traverses the file tree depth-first starting at the given directory.
    3. walk(Path dir, int maxDepth): Same as walk(dir) but with a maximum depth.
  • Ranges

    There are also new methods for creating ranges of numbers as Streams.

    For example, the static method, range, on the IntStream interface:

    The above would print out the numbers one through ten.

    Each primitive Stream (IntStream, DoubleStream, and LongStream) has a corresponding range method

For Each

The most basic thing you can do with a Stream is loop through it using the forEach method.

For example, to print out all of the files in the current directory, you could do the following:

For the most part, this replaces the “for loop”. It is more concise, and more object-oriented since you are delegating the implementation of the actual loop.

Comparisons to Java 7

To better illustrate the benefit of Streams in Java 8, here are some examples of code from Java 7 compared to their new versions.

Finding a maximum

Calculating an average

Printing the numbers one through ten

Conclusions

The Stream API is a powerful but simple to understand set of tools for processing sequence of elements. It allows us to reduce a huge amount of boilerplate code, create more readable programs and improve app’s productivity when used properly.

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

2 Thoughts on “Stream API in Java 8”

Leave a Reply

Your email address will not be published. Required fields are marked *