In Java 9, following methods are added to Stream interface of stream package. These methods are default and static.
Table of Contents
The takeWhile() Method
This method is included in Stream interface and used to collect the elements into a stream. It takes all the elements till the condition specified as predicate.
For example, we want to collect a few elements from a stream of elements; for this purpose, we can use takeWhile()
method.
The following is the syntax of the method.
1 2 3 |
default Stream<T> takeWhile(Predicate<? super T> predicate) |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main{ public static void main(String[] args){ List<Integer> list = Stream.of(20,30,40,62,85,21) .takeWhile(i -> (i<85)).collect(Collectors.toList()); System.out.println(list); // collecting into a list // traverse elements Stream<Integer> stream = Stream.of(20,30,40,62,85,21); stream.takeWhile(num -> num < 85).forEach(num -> System.out.println(num)); } } |
Output
30
40
62
The dropWhile() Method
This method is just opposite of takeWhile()
. It drops the result when the specified condition occurs and takes rest of elements.
If stream is ordered then it returns a stream that contains remaining elements after dropping the elements. If the stream is unordered, it returns a stream that includes the remaining elements after dropping a subset of elements. The following is the syntax of the method.
1 2 3 |
default Stream<T> dropWhile(Predicate<? super T> predicate) |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package myjavaproject; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main{ public static void main(String[] args){ List<Integer> list = Stream.of(20,30,40,62,85,21) .dropWhile(i -> (i<85)).collect(Collectors.toList()); System.out.println(list); // collecting into a list // traverse elements Stream<Integer> stream = Stream.of(20,30,40,62,85,21); stream.dropWhile(num -> num < 85).forEach(num -> System.out.println(num)); } } |
Output
21
The ofNullable() Method
This method is used for null checking and returns either a sequential Stream of a single element or an empty Stream
. The syntax of the method is given below.
1 2 3 |
static <T> Stream<T> ofNullable(T t) |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.stream.Stream; public class Main{ public static void main(String[] args){ Stream<Integer> stream = Stream.ofNullable(12); stream.forEach(System.out::print); stream = Stream.ofNullable(null); stream.forEach(System.out::print); // empty stream } } |
Output
The iterate() Method
This method is added into Java 9 that helps to iterate over a sequence of stream and takes three arguments. The first argument is called seed and used to initialize a stream, the second argument is called predicate and used to specify the condition while third argument is used to generate next elements.
The syntax of the method is given below.
1 2 3 |
static <T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next) |
Example
1 2 3 4 5 6 7 8 9 10 |
import java.util.stream.Stream; public class Main{ public static void main(String[] args){ Stream<Integer> stream = Stream.iterate(1, a->a<5, a->a+1); stream.forEach(System.out::println); } } |
Output
2
3
4
This is similar to writing a for loop as below:
1 2 3 4 5 6 |
for(int i=1;i<5;i++) { System.out.println(i) } |
That’s all about Java 9 Stream API improvements.