Java 9 Enhancements to Stream and java.util.Arrays

In this tutorial we will cover the Java 9 enhancements to Stream and java.util.Arrays. The most important enhancements to stream are the takeWhile, dropWhile, ofNullable, and iterate methods. The enhancement to Arrays has been the new equals and mismatch method that allow comparing subset of arrays.

We will look into the details of the new methods below.

Java 9 Enhancements to Stream

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

For ordered stream this method returns the longest prefix of the stream that matches the predicate function. For unordered stream the method returns a subset of elements of the stream that match the given predicate. For unordered stream the function may return any subset that matches the given predicate. This method can be best explained by an example.

jshell< List<Integer> a=List.of(1,2,3,4,5,6,7,8,9)
a ==> [1, 2, 3, 4, 5, 6, 7, 8, 9]
jshell> a.stream().takeWhile(a->{return a<5;}).collect(Collectors.toList())

In the example above, we create a stream of elements 1 to 9. The takeWhile operation then ‘takes’ a subset from this list such that all elements in the subset are allowed by the predicate (i.e. subset of all elements less than 5 )

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

In Contrast to takeWhile, the dropWhile operation drops the longest prefix of the stream that matches the given predicate and returns the other elements.

a ==> [1, 2, 3, 4, 5, 6, 7, 8, 9]
jshell> a.stream().dropWhile(a->{return a<5;}).collect(Collectors.toList())
$11 ==> [5, 6, 7, 8, 9]

static<T> Stream<T> ofNullable(T t)

This method returns a stream containing a single element if t is not null and zero elements if t is null.

jshell> Stream.ofNullable(null).count()
$18 ==> 0

jshell> Stream.ofNullable(1).count()
$19 ==> 1 

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

The iterate function is used to return a stream whose elements are obtained by iteratively applying the next function to the previous element (starting with the seed element) as long as the hasNext predicate returns true. Here’s an example

jshell> Stream.iterate(1L, n->{ return n<5;}, n -> { return n + 1;})
                                .collect(Collectors.toList())
$3 ==> [1, 2, 3, 4]

In the example above we increment the previous element by 1 (next => n +1) as long as n < 5 (hasNext). The first element is 1 (seed). We can even generate an infinite stream.

jshell> Stream.iterate(1L, n -> { return n + 1;}).limit(10)
                               .collect(Collectors.toList())
$4 ==> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

We now look into the new methods added in java.util.Arrays

Java 9 enhancements to java.util.Arrays

Java 9 adds a suite of equals methods to Arrays. This article looks at some of those methods

static boolean equals(double[] a, int aFromIndex, int aToIndex,
double[] b, int bFromIndex, int bToIndex)

This method returns true if two arrays are equal over the specified range.
example:

jshell> Arrays.equals(new double[] {8,1,2,3,4,5},1,4,new double[]{5,1,2,3,6,7},1,4)
$14 ==> true

In the example above we compare the first to fourth element of two arrays.
if we include the fifth element in the comparison, the method should return false.

jshell> Arrays.equals(new double[] {8,1,2,3,4,5},1,5,new double[]{5,1,2,3,6,7},1,5)
$15 ==> false

It is possible to compare different subsets (of same length) from each array

jshell> Arrays.equals(new double[] {8,1,2,3,4,5},1,4,new double[]{3,5,1,2,3,6,7},2,5)
$16 ==> true

In this example we compare the first to fourth elements for first array to the 2nd to 5th elements of the second array.

static int mismatch(double[] a, int aFromIndex, int aToIndex,
double[] b, int bFromIndex, int bToIndex)

Returns the index of the first mismatch between two arrays over the specified index ranges.Returns -1 if there is no mismatch

jshell> Arrays.mismatch(new double[]{1,2,3,5,6,7},new double[]{1,2,3})
$46 ==> 3
jshell> Arrays.mismatch(new double[]{1,2,3,5,6,7},0,2,new double[]{1,2,3},0,2)
$47 ==> -1

There are equivalent methods for other types too. They are similar to the ‘double’ methods that we have covered here.

This completes our tutorial on the Java 9 Enhancements to stream and java.util.arrays. In the next article we will look at the stack walking API.

Leave a Comment