Java 9 Streams Enhancements

Gowgi
169 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Java 9 Streams Enhancements - Gowtham Girithar Srirangasamy

Java 8 introduced Streams, which help developers perform aggregate operations from a sequence of objects. Meanwhile, Java 9 introduced a few more utility methods to make developers' work even easier.

DropWhile

Dropwhile will throw away the elements at the start where the predicate is true. For example, it returns a Stream consisting of the remaining elements of this Stream after dropping the longest prefix of elements that match the given predicate (ordered Stream).In an unordered case, return the subset of elements as per their definition.

Streams - dropWhile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.demo.java9;
import java.util.List;
import java.util.stream.Stream;
/**
*
* @author Gowtham Girithar Srirangasamy
* The code to explain the dropWhile in java 9.
*/
public class DemoDropWhile {
public static void main(String[] args){
Stream.of("apple", "banana", "cat", "", "elephant","","Dog")
.dropWhile(s->!s.isEmpty()).forEach(System.out::println);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

TakeWhile

Takewhile will stop once it has found an element it fails to match, like a Stream consisting of the longest prefix of elements taken from this Stream that matches the given predicate (ordered Stream).In an unordered case, return the subset of elements as per their definition.

Streams - takeWhile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.demo.java9;
import java.util.List;
import java.util.stream.Stream;
/**
*
* @author Gowtham Girithar Srirangasamy
* The code to explain the takewhile in java 9.
*/
public class DemoTakeWhile {
public static void main(String[] args){
Stream.of("apple", "banana", "cat", "", "elephant","","Dog")
.takeWhile(s->!s.isEmpty()).forEach(System.out::println);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Iterate

Streams has the iterate method with two arguments: One is the initializer, which is called as a seed, and the other argument is the function to be applied to the previous element to produce the new element. But the drawback of that method is that there is no termination of that loop. Java 9 updated the iterate method, which is similar to what we had earlier for loop-based iterations. This is the replacement of for loops. This also solves the problem that we had in the earlier iterate method.

Streams - iterate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.demo.java9;
import java.util.List;
import java.util.stream.Stream;
import java.util.stream.IntStream;
/**
*
* @author Gowtham Girithar Srirangasamy
* The code to explain the iterate in java 9.
*/
public class DemoIterate {
public static void main(String[] args){
IntStream.iterate(3, x -> x < 10, x -> x + 3)
.forEach(System.out::println);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

ofNullable

Java 9 introduced the ofNullable method to return empty Optionals if the value is null. The main intention is to avoid NullPointerExceptions and to avoid having null checks everywhere.

Streams - ofNullable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.demo.java9;
import java.util.List;
import java.util.stream.Stream;
/**
*
* @author Gowtham Girithar Srirangasamy
* The code to explain the ofNullable in java 9.
*/
public class DemoOfNullable {
public static void main(String[] args){
Long count =Stream.ofNullable(100).count();
Long countNull =Stream.ofNullable(null).count();
System.out.println("Count "+count);
System.out.println("Count null "+countNull);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

In usecases we are writing the code like below Employee emp= getEmployee(empId);

Stream roles= emp== null? Stream.empty(): emp.roles();

This can be re-written as

Employee emp= getEmployee(empId);

Stream.ofNullable(emp).flatMap(Employee::roles)

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content