In this article we will see Java 9 Stream api improvements i.e. takeWhile
, dropWhile
, ofNullable
and iterate
methods with examples. Java 9 has come up with lots of changes and new features as well to existing APIs.
Java 9 Stream interface has the following new methods.
default Stream dropWhile(Predicate<? super T> predicate)
-
default Stream takeWhile(Predicate<? super T> predicate)
static Stream ofNullable(T t)
static Stream iterate(T seed, Predicate<? super T> hasNext, UnaryOperator next)
1. Java 9 Stream api improvements with examples
1.1. Stream dropWhile
- The method
dropWhile
drops elements in the stream until it encounters the first element that doesn’t match the predicate. - For unordered stream the behavior of
dropWhile()
non-deterministic. The current implementation drops the matching elements from the beginning until it finds a non matching element. If the first element does not match the predicate, thedropWhile
method returns original stream. If all elements match the predicate, the method returns an empty stream.
Stream dropWhile() method Example :
If you run couple of times the following example, for the unordered List results might be different each time.
public class StreamDropWhileDemo { public static void main(String[] args) { // Ordered List List<Integer> itemList = List.of(10, 12, 14, 16, 17, 19, 20, 23, 27, 30, 31, 33); List<Integer> list1 = itemList.stream() .dropWhile(i -> i % 2 == 0).collect(Collectors.toList()); System.out.println("ordered list dropWhile(i -> i % 2 == 0) => " + list1); // unOrdered List Set<Integer> unOrderedList = Set.of(33, 30, 14, 27, 17, 19, 20, 23, 16, 12, 31, 10); List<Integer> list2 = unOrderedList.stream() .dropWhile(i -> i % 2 == 0).collect(Collectors.toList()); System.out.println("unordered list dropWhile(i -> i % 2 == 0) => " + list2); } }
Output Results :
ordered list dropWhile(i -> i % 2 == 0) => [17, 19, 20, 23, 27, 30, 31, 33] unordered list dropWhile(i -> i % 2 == 0) => [33, 31, 30, 27, 23, 20, 19, 17, 16, 14]
1.2. Stream takeWhile
- The method
takeWhile
performs the opposite action on a stream as todropWhile
.takeWhile()
takes elements in the stream until it it matches the first element that doesn’t match the predicate. takeWhile()
has the same behavior asdropWhile()
when processing ordered and unordered streams.
Stream takeWhile() method Example :
If you run couple of times the following example, for the unordered List results might be different each time for takeWhile
also.
public class StreamTakeWhileDemo { public static void main(String[] args) { // Ordered List List<Integer> itemList = List.of(10, 12, 14, 16, 17, 19, 20, 23, 27, 30, 31, 33); List<Integer> list1 = itemList.stream() .takeWhile(i -> i % 2 == 0).collect(Collectors.toList()); System.out.println("ordered list takeWhile(i -> i % 2 == 0) => " + list1); // unOrdered List Set<Integer> unOrderedList = Set.of(33, 30, 14, 27, 17, 19, 20, 23, 16, 12, 31, 10); List<Integer> list2 = unOrderedList.stream() .takeWhile(i -> i % 2 == 0).collect(Collectors.toList()); System.out.println("unordered list takeWhile(i -> i % 2 == 0) => " + list2); } }
Output Results :
ordered list takeWhile(i -> i % 2 == 0) => [10, 12, 14, 16] unordered list takeWhile(i -> i % 2 == 0) => [20]
1.3. Stream ofNullable
- The method
ofNullable
returns a Stream of zero or one element, depends on if the input value isnull
. - Below example demonstrates how to get non-null values from a list using
ofNullable
method.
Stream ofNullable method Example :
public class StreamTakeWhileDemo { public static void main(String[] args) { System.out.println("--------ofNullable() example1----------"); List<String> alist = new ArrayList<>(); alist.add("Peter"); alist.add("Gerhard"); alist.add(null); alist.add("Satheesh"); alist.add(null); alist.add("Philip"); Set<String> nonNullValues = alist.stream() .flatMap(e -> Stream.ofNullable(e)).collect(Collectors.toSet()); System.out.println("List including null values: " + alist); System.out.println("Non-null Values in List: " + nonNullValues); System.out.println("--------ofNullable() example2----------"); System.out.println("non null => "+Stream.ofNullable("Peter").count()); System.out.println("null => "+Stream.ofNullable(null).count()); } }
Output Results :
--------ofNullable() example1---------- List including null values: [Peter, Gerhard, null, Satheesh, null, Philip] Non-null Values in List: [Gerhard, Peter, Philip, Satheesh] --------ofNullable() example2---------- non null => 1 null => 0
1.4. Java 9 Stream api iterate
- The
Stream
class static methoditerate(T seed, Predicate<? super T> hasNext, UnaryOperator next)
is a new way to generate streams using the iterator pattern. seed
is the initial element for iterationhasNext
is the predicate that applies to the current element to check if the stream should be terminated,next
is the function that applies to the current element to produce the next element
in the stream.- If the initial element
seed
doesn’t match the predicatehasNext
, the result stream is empty.
java Stream iterate method Example :
public class StreamTakeWhileDemo { public static void main(String[] args) { System.out.println("--------iterate() example----------"); List<Integer> oddList = Stream.iterate(1, i -> i <= 10, i -> i + 2).collect(Collectors.toList()); System.out.println("Odd nunmbers between 1 to 10: " + oddList); List<Integer> evenList = Stream.iterate(2, i -> i <= 10, i -> i + 2).collect(Collectors.toList()); System.out.println("Even nunmbers between 1 to 10: "+evenList); System.out.println("--------when seed doesnt match predicate----------"); List<Integer> resultList = Stream.iterate(15, i -> i <= 10, i -> i + 2).collect(Collectors.toList()); System.out.println("Even nunmbers between 1 to 10: "+resultList); } }
Output Results :
--------iterate() example---------- Odd nunmbers between 1 to 10: [1, 3, 5, 7, 9] Even nunmbers between 1 to 10: [2, 4, 6, 8, 10] --------when seed doesnt match predicate---------- Even nunmbers between 1 to 10: []
Conclusion
In this article we have covered Java 9 Stream api improvements with examples of takeWhile
, dropWhile
, ofNullable
and iterate
methods.