In this post, We will talk and learn about Java 9 Stream API improvements using an example
Java 9 added the new APIs for Stream.
Java Stream Iterate Method
- A new overloaded iterate method is added in Java 9 stream interface. This function usually help us to iterate stream elements till the specified condition is true.
- It takes three arguments, seed, hasNext, and next.
public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> uo)
1 2 |
The <strong>seed</strong> is the initial element,<span style="font-size: 12pt;"><strong>UnaryOperator<T> uo</strong></span> a function to be applied to the previous element to produce a new element and It returns a new sequential Stream |
Java Stream takeWhile() Method
- Stream’s method takeWhile takes each element that matches its predicate. It stops when it gets the unmatched element. It returns a subset of elements that contains all matched elements, another part of the stream is discarded.
default Stream<T> takeWhile(Predicate<? super T> predicate)
This function returns if this stream is ordered, a stream to consist of the longest prefix of elements taken from this stream that match the given predicate.
Java Stream dropWhile() Method
- The dropWhilemethod does the opposite of the takeWhile method
default Stream<T> dropWhile(Predicate<? super T> predicate)
Java 9 Stream ofNullable Method
- Until Java 8, we cannot have null value in a stream. It would have caused NullPointerException.
- In Java 9, the ofNullable function lets us create a single-element stream that holds a value if not null or is an empty stream otherwise.
- Technically, ofNullable()is very similar to null condition check, when talking in the context of stream API.
public static<T> Stream<T> ofNullable(T t)
This method returns a sequential Stream
containing a single element and if non-null otherwise returns an empty Stream
.
Java9StreamImprovementsDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<span style="font-size: 14pt;">package com.kkhindigyan.cleint; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Java9StreamImprovementsDemo { public static void main(String[] args) { System.out.println("Stream.iterate----------------------------------------------"); List<Integer> collect1 = Stream.iterate(1, i->i+1).limit(10).collect(Collectors.toList()); collect1.forEach(System.out::println); System.out.println("-------------------------------"); List<Integer> collect2 = Stream.iterate(1,i->i<=10, i->i+1).collect(Collectors.toList()); collect2.forEach(System.out::println); System.out.println("Stream.takeWhile----------------------------------------------"); List<Integer> collect3 = Stream.of(12,4,80,5,67,8,9,9,0,90).takeWhile(numner->numner%2 == 0) .collect(Collectors.toList()); collect3.forEach(System.out::println); System.out.println("Stream.dropWhile----------------------------------------------"); List<Integer> collect4 = Stream.of(12,4,80,5,67,8,9,9,0,90).dropWhile(numner->numner%2 == 0) .collect(Collectors.toList()); collect4.forEach(System.out::println); System.out.println("Stream.ofNullable----------------------------------------------"); Stream<Integer> s1 = Stream.ofNullable(20); Stream<Integer> s2 = Stream.ofNullable(null); System.out.println(s1.count()); System.out.println(s2.count()); } } </span> |
If you run ClientTest.java as Java Application then it will give the below output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<span style="font-size: 14pt;">Stream.iterate---------------------------------------------- 1 2 3 4 5 6 7 8 9 10 ------------------------------- 1 2 3 4 5 6 7 8 9 10 Stream.takeWhile---------------------------------------------- 12 4 80 Stream.dropWhile---------------------------------------------- 5 67 8 9 9 0 90 Stream.ofNullable---------------------------------------------- 1 0 </span> |
That’s all about Java 9 Stream API improvements
You May Also Like:
Java 9 Jshell Tutorial
Java 9 private Methods in Interfaces
Java 9 Try with Resource Enhancement
Java 9 @SafeVarargs Annotation
Java 9 Collection Factory Methods
If you have any feedback or suggestion please feel free to drop in below comment box.