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
  • java9_stream_api-450x167

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.

Let us perform some Stream operations now.

Example:-

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:-

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:-

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:-

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:-

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:-

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:-

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:-

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:-

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 8: IntStream iterate and fileter Example:-

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:-

NOTE:-
The sub-interfaces of Stream (like IntStream, LongStream etc) inherits all these 4 new methods.

That’s it all about “Java SE 9: Stream API Improvements” new feature. We will discuss some more Java SE 9 New Features in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions/type errors.

Thank you for reading my tutorials.

Happy Java SE 9 Learning!

By admin

Leave a Reply

%d bloggers like this: