In this tutorial, we’re going to take a first look at the streams API updates in Java 9, which includes new stream operations: dropWhile, takeWhile, iterate and ofNullable.

1. The dropWhile() Method

Firstly, let’s take a look at the syntax of the dropWhile() method:

Parameters:
predicate – a non-interfering, stateless predicate to apply to elements to determine the longest prefix of elements.
Returns:
the new stream

Next, let’s get to an example which we have an ordered stream of marks and we want to drop all marks that are less than 5:

Running the example will give us the output:

Notice that because this stream is ordered, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.

Now, let’s take a look at another example which we have an unordered stream of marks and we’ll use the same predicate with above example to drop all marks less than 5:

If we run the example, we will get the output as follows:

We can see that there are still several marks less than 5 but were not dropped.

Notice that if a stream is unordered, and some (but not all) elements of this stream match the given predicate, then the behavior of the dropWhile() method is non-deterministic; it is free to drop any subset of matching elements (which includes the empty set). With the current implementation, we can see that the dropWhile() method drops the matching elements from the beginning until it finds a non-matching element.

2. The takeWhile() Method

Another method introduced in Java 9 is the takeWhile() method:

It returns a stream consisting of a subset of elements taken from a stream that matches the given predicate.

Next, let’s get to an example of the takeWhile() method. In this example, we have an ordered stream of marks and we want to take all marks that are less than 5:

Running the example will print out to the console:

Notice that because the above stream is ordered then the longest prefix is a contiguous sequence of elements of this stream that match the given predicate. The first element of the sequence is the first element of this stream, and the element immediately following the last element of the sequence does not match the given predicate.

Let’s get to another example which we have an unordered stream of marks and we’ll use the same predicate with above example to take all marks less than 5:

Running the example will give us the output:

We can see that there are still several marks less than 5 but were not taken. This is because with the takeWhile() method, if a stream is unordered, and some (but not all) elements of the stream match the given predicate, then the behavior of this operation is nondeterministic; it is free to take any subset of matching elements (which includes the empty set). With the current implementation, we can see that the takeWhile() method takes the matching elements from the beginning until it finds a non-matching element.

3. The iterate() Method

Before getting to the new iterate() method in Java 9, let’s take a look at the old iterate() method introduced in Java 8:

The method returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.

For example,

If we run this example, our program will never terminate, it keeps on adding 1 in a loop indefinitely. To improve this situation, Java 9 introduces a new version of the iterate() method, which takes a predicate as its second argument to apply to elements to determine when the stream must terminate:

For example:

The example will print out a sequense of numbers from 0 to 99 then terminates.

4. The ofNullable() Method

The last method added to the Stream interface from Java 9 is the ofNullable() method:

The ofNullable() method returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream. For example:

5. Conclusions

The tutorial has illustrated us about streams API updates in Java 9. We just got to new operations added to the Stream interface such as dropWhile, takeWhile, iterate and ofNullable which facilitate us in manipulating with stream in Java.

Below are other related tutorials for your references:

Java 9 Tutorial

Install Oracle Java 9 on CentOS, RHEL 7

Install Oracle Java 9 on Ubuntu 16.04 LTS (Xenial Xerus)

Set Up Eclipse, IntelliJ And NetBeans For Java 9

Java 9 Example With Maven And JUnit 5

Java 9 JShell Cheat Sheet

Create Immutable Lists In Java 9 By Static Factory Methods

Private Interface Methods In Java 9

Using The InputStream.transferTo() To Copy Streams In Java 9

Java 9 – New Methods Of The Optional Class

How To Compare Arrays In Java 9

Java 9 HTTP 2 Client API Example

Java 9 – Effectively Final Variables In try-with-resources

0 0 vote
Article Rating