Java Stream API Improvements: Enhancing Stream Functionality in Java

Discover the enhancements made to the Java Stream API in Java 9, which introduced new methods for more efficient and convenient aggregate operations on sequences of objects. Learn how these improvements streamline data processing and boost productivity for developers.



Java - Stream API Improvements

Java introduced the Stream API to help developers perform aggregate operations on a sequence of objects. With Java 9, new methods were added to enhance stream functionalities, making them even more efficient and convenient to use.

takeWhile(Predicate Interface) Method

The takeWhile method processes the elements of the stream until the given predicate returns false. It returns, in the case of an ordered stream, a new stream consisting of the longest prefix of elements that match the provided predicate.

Syntax


default Stream<T> takeWhile(Predicate<? super T> predicate)

Example

In this example, the takeWhile method processes values from the stream until it encounters an empty string, at which point it stops.


package com.example;

import java.util.stream.Stream;

public class Tester {
public static void main(String[] args) {
    Stream.of("a", "b", "c", "", "e", "f")
        .takeWhile(s -> !s.isEmpty())
        .forEach(System.out::print);		 
}
}

Output

abc

dropWhile(Predicate Interface) Method

The dropWhile method discards elements from the start of the stream as long as they match the given predicate. Once the predicate returns false, it processes the remaining elements.

Syntax


default Stream<T> dropWhile(Predicate<? super T> predicate)

Example

In this example, the dropWhile method skips values at the beginning of the stream until it finds an empty string, then processes the remaining values.


package com.example;

import java.util.stream.Stream;

public class Tester {
public static void main(String[] args) {
    Stream.of("a", "b", "c", "", "e", "f")
        .dropWhile(s -> !s.isEmpty())
        .forEach(System.out::print);
    
    System.out.println();
    
    Stream.of("a", "b", "c", "", "e", "", "f")
        .dropWhile(s -> !s.isEmpty())
        .forEach(System.out::print);
}
}

Output

ef
ef

iterate Method

The iterate method now includes a hasNext predicate as a parameter, allowing the loop to stop once the predicate returns false.

Syntax


static <T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)

Example

This example demonstrates how the iterate method uses the hasNext predicate to stop the iteration when the value reaches or exceeds 10.


package com.example;

import java.util.stream.IntStream;

public class Tester {
public static void main(String[] args) {
    IntStream.iterate(3, x -> x < 10, x -> x + 3)
        .forEach(System.out::println);
}
}

Output

3
6
9

ofNullable Method

The ofNullable method prevents NullPointerException by returning a stream containing a single element if the provided object is non-null, or an empty stream if the object is null.

Syntax


static <T> Stream<T> ofNullable(T t)

Example

This example shows how ofNullable behaves with both non-null and null values.


package com.example;

import java.util.stream.Stream;

public class Tester {
public static void main(String[] args) {
    long count = Stream.ofNullable(100).count();
    System.out.println(count);
    
    count = Stream.ofNullable(null).count();
    System.out.println(count);
}
}

Output

1
0