Open Source Your Knowledge, Become a Contributor

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

packagecom.demo.java9;

importjava.util.List;

importjava.util.stream.Stream;

/**

*

* @author Gowtham Girithar Srirangasamy

* The code to explain the dropWhile in java 9.

*/

publicclassDemoDropWhile {

publicstaticvoidmain(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

packagecom.demo.java9;

importjava.util.List;

importjava.util.stream.Stream;

/**

*

* @author Gowtham Girithar Srirangasamy

* The code to explain the takewhile in java 9.

*/

publicclassDemoTakeWhile {

publicstaticvoidmain(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

packagecom.demo.java9;

importjava.util.List;

importjava.util.stream.Stream;

importjava.util.stream.IntStream;

/**

*

* @author Gowtham Girithar Srirangasamy

* The code to explain the iterate in java 9.

*/

publicclassDemoIterate {

publicstaticvoidmain(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

packagecom.demo.java9;

importjava.util.List;

importjava.util.stream.Stream;

/**

*

* @author Gowtham Girithar Srirangasamy

* The code to explain the ofNullable in java 9.

*/

publicclassDemoOfNullable {

publicstaticvoidmain(String[] args){

Longcount=Stream.ofNullable(100).count();

LongcountNull=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);