web
 

Java SE 9: Stream API Improvements


Post Brief Table of Content:

  • Introduction
  • Java SE 8: Stream API Basics
  • Java SE 9: Stream API Improvements
  • Java SE 9: Stream API takeWhile() Method
  • Java SE 9: Stream API dropWhile() Method
  • Java SE 9: Stream API iterate() Method
  • Java SE 9: Stream API ofNullable() Method
Introduction

Oracle Corporation is going to release Java New Version: Java SE 9 around End of March 2017. So, I would like to deliver a series of Posts on Java SE 9 New Features. It’s my ninth post in this series.

In this post, first we will discuss about Java SE 8 Stream API basics, then will introduce Java SE 9 Stream API Improvements. Let us start with Stream API basics in next section.

Java SE 8: Stream API Basics

As we know, one of the big changes Oracle Corp has introduced in Java SE 8 release is Stream API. The other big change is Lambda Expressions.

What is a Stream?
A Stream is a sequence of elements and supports a set of aggregate operations on them easily. It supports those operations either in sequential or parallel way depends on our requirements. Oracle Corp has defined this New Stream API under java.util.stream package.

The important thing in this API is Stream interface. The main advantage of this Stream API is that it supports Internal Iteration. Because of this new feature in Stream API, we can perform our operations in Lazy and Parallel way. Example:-


jshell> import java.util.stream.*

jshell> Stream stream = Stream.of(1,2,3,4,5,6,7,8,9,10)
stream ==> java.util.stream.ReferencePipeline$Head@64bfbc86

jshell> stream.forEach(x -> System.out.println(x));
1
2
3
4
5
6
7
8
9
10

To read more about Java SE 8 Stream API basics, please go through this tutorial: Java 8 Stream.

Java SE 9: Stream API Improvements

In Java SE 9, Oracle Corp has added the followng four useful new methods to java.util.Stream interface.

  • dropWhile
  • takeWhile
  • iterate
  • ofNullable

As Stream is an interface, first two new methods are default methods and last two are static methods. Two of them are very important: dropWhile and takeWhile methods.

If you are familiar with Scala Language or any Functions programming language, you will definitely know about these methods. These are very useful methods in writing some functional style code. Let us discuss them one by one with description and useful examples in coming sections.

Java SE 9: Stream API takeWhile() Method

In Stream API, takeWhile() Method returns longest prefix elements which matches the Predicate condition.

It takes a Predicate as an argument. A Predicate is Boolean expression which returns either true or false. It behaves differently for Ordered and Unordered Streams. Let us explore them with some simple examples below.

Stream API:-


default Stream<T> takeWhile(Predicate<? super T> predicate)

If Stream is an Ordered, then takeWhile method returns the longest prefix which matches that Predicate. The resulted Stream contains only that prefix elements which matches that Predicate condition.

Ordered Stream Example:-


jshell> Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10)
stream ==> java.util.stream.ReferencePipeline$Head@55d56113

jshell> stream.takeWhile(x -> x < 4).forEach(a -> System.out.println(a))
1
2
3

As this Stream is an Ordered, takeWhile() method returns first three elements which matches our Predicate. Here our Predicate is that “Element must be less than 4”.

Unordered Stream Example:-


jshell> Stream<Integer> stream = Stream.of(1,2,4,5,3,6,7,8,9,10)
stream ==> java.util.stream.ReferencePipeline$Head@55d56113

jshell> stream.takeWhile(x -> x < 4).forEach(a -> System.out.println(a))
1
2

As this Stream is an Unordered, takeWhile() method returns first two elements which matches our Predicate.

That means takeWhile() returns all prefixed elements until they match Predicate condition. When that Predicate returns false for first element, then it stops evaluation and returns that subset elements. That Predicate is evaluated until that returns false for first time.

Java SE 9: Stream API dropWhile Method

In Stream API, dropWhile() Method drops the longest prefix elements which matches the Predicate and returns the rest of elements.

It takes a Predicate as an argument. A Predicate is Boolean expression which returns either true or false. It behaves differently for Ordered and Unordered Streams. Let us explore them with some simple examples below.

Stream API:-


default Stream<T> dropWhile(Predicate<? super T> predicate)

If Stream is an Ordered, then dropWhile method drops the longest prefix elements which matches that Predicate and returns the rest of elements. The resulted Stream contains all elements except those prefixed elements which matches the Predicate condition.

Ordered Stream Example:-


jshell> Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10)
stream ==> java.util.stream.ReferencePipeline$Head@55d56113

jshell> stream.dropWhile(x -> x < 4).forEach(a -> System.out.println(a))
4
5
6
7
8
9
10

As this Stream is an Ordered, dropWhile() method drops first three elements which matches our Predicate and returns rest of the elements into resulted Stream. Here our Predicate is that “Element must be less than 4”.

Unordered Stream Example:-


jshell> Stream<Integer> stream = Stream.of(1,2,4,5,3,6,7,8,9,10)
stream ==> java.util.stream.ReferencePipeline$Head@55d56113

jshell> stream.dropWhile(x -> x < 4).forEach(a -> System.out.println(a))
4
5
3
6
7
8
9
10

As this Stream is an Unordered, dropWhile() method drop first two elements only which matches our Predicate and returns rest of the elements into resulted Stream.

That means dropWhile() first drops all prefixed elements until they match Predicate condition. When that Predicate returns false for first element, then it stops evaluation and returns the rest of subset elements into resulted Stream.

Java SE 9: Stream API iterate Method

In Stream API, iterate() returns a Stream of elements which start with initialValue (first parameter), matches the Predicate (2nd parameter) and generate next element using 3rd parameter.

Stream API:-

Copy
static <T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)

It is similar to for-loop: First parameter is init value, next parameter is condition and final parameter is to generate next element (for instance, increment or decrement operation).

Java SE 9 IntStream iterate Example:-

Copy
jshell> IntStream.iterate(2, x -> x < 20, x -> x * x).forEach(System.out::println) 2 4 16

Here Stream starts with element “2”, then perform condition “2 < 20" is true, prints value. In next iteration, increment value "2 * 2 = 4", check condition "4 < 20" is true, prints value. And so on until condition return false value.


  Java SE 9's iterate() = Java SE 8's iterate() + Java SE 8's filter()

Java SE 8: IntStream iterate and fileter Example:-


jshell> IntStream.iterate(2, x -> x * x).filter(x -> x < 20).forEach(System.out::println)
2
4
16

Java SE 9: Stream API ofNullable Method

In Stream API, ofNullable() returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.

Java SE 9 Example:-










journaldev is optimized for learning.© journaldev .
All Right Reserved and you agree to have read and accepted our term and condition