Last updated on February 27th, 2024
Stream API was introduced in Java 8. In Java 9 new four methods are added in Stream API.The new four methods are takeWhile(), dropWhile(), iterate(), and ofNullable().
Table of content
- Java 9 takeWhile() method
- Java 9 dropWhile() method
- Java 9 iterate() method
- Java 9 ofNullable() method
- Conclusion
1. Java 9 takeWhile() method
This is a default method added in Stream Interface in Java 9. This method accepts all the values until the predicate returns false.
Syntax
default Stream<T> takeWhile(Predicate<? super T> predicate)
Example
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> al= new ArrayList<Integer>();
al.add(2);
al.add(4);
al.add(1);
al.add(5);
al.add(6);
al.add(8);
System.out.println("Initial List:"+al);
List<Integer> l1= al.stream().filter(i->i%2==0).collect(Collectors.toList());
System.out.println("After filter() List:"+l1);
List<Integer> l2= al.stream().takeWhile(i->i%2==0).collect(Collectors.toList());
System.out.println("After takeWhile() List:"+l2);
}
}
Output
Initial List:[2, 4, 1, 5, 6, 8]
After filter() List:[2, 4, 6, 8]
After takeWhile() List:[2, 4]
2. Java 9 dropWhile() method
This is a default method added in Stream Interface in Java 9. This method throws away all the values at the start until the predicate returns true.
Syntax
default Stream<T> dropWhile(Predicate<? super T> predicate)
Example
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> al= new ArrayList<Integer>();
al.add(2);
al.add(4);
al.add(1);
al.add(5);
al.add(6);
al.add(8);
System.out.println("Initial List:"+al);
List<Integer> l1= al.stream().filter(i->i%2==0).collect(Collectors.toList());
System.out.println("After filter() List:"+l1);
List<Integer> l2= al.stream().dropWhile(i->i%2==0).collect(Collectors.toList());
System.out.println("After dropWhile() List:"+l2);
}
}
Output
Initial List:[2, 4, 1, 5, 6, 8]
After filter() List:[2, 4, 6, 8]
After dropWhile() List:[1, 5, 6, 8]
3. Java 9 iterate() method
This is a static method added in Stream Interface in Java 9. This method is also available with two arguments but from Java 9 onwards this method with three arguments is also available. It allows us to iterate stream elements till the specified condition.
Syntax
static <T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)
Example
import java.util.stream.Stream;
public class Test{
public static void main(String[] args) {
Stream.iterate(1, i -> i <= 10, i -> i*4)
.forEach(System.out::println);
}
}
Output
1
4
4. Java 9 ofNullable() method
This is also a static method added in Stream Interface in Java 9. This method is introduced to prevent NullPointerExceptions and to avoid null checks for streams. It returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
Syntax
static <T> Stream<T> ofNullable(T t)
Example
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
long count = Stream.ofNullable(10).count();
System.out.println(count);
count = Stream.ofNullable(null).count();
System.out.println(count);
}
}
Output
1
0
5. Conclusion
In this topic, we learned about Stream API enhancement in Java 9.