Java 9 - Stream API Improvements with Examples

In this article, we will learn what are the Stream API improvements made in Java 9 with an example.
Streams were introduced in Java to help developers perform aggregate operations from a sequence of objects. With Java 9, few more methods are added to make streams better.
Java 9 improvements in Stream API:
  • takeWhile(Predicate Interface)
  • dropWhile(Predicate Interface)
  • iterate()
  • ofNullable()
Let's learn each of these methods with examples.
Learn about Java 8 Streams at https://www.javaguides.net/p/java-8-stream-api-tutorial.html

Stream takeWhile(Predicate Interface)

The takeWhile() method takes all the values until the predicate returns false. It returns, in case of ordered stream, a stream consisting of the longest prefix of elements taken from this stream matching the given predicate.
Example:
package net.javaguides.corejava.java9;

import java.util.stream.Stream;

/**
 * Demonstrates Java 9 Stream API Improvements
 * 
 * @author Ramesh Fadatare
 *
 */
public class Java9StreamAPIExample {
    public static void main(String[] args) {
        Stream.of("a", "b", "c", "", "e", "f").takeWhile(s - > !s.isEmpty())
            .forEach(System.out::print);

        System.out.println();

        Stream.of("a", "", "c", "", "e", "f").takeWhile(s - > !s.isEmpty())
            .forEach(System.out::print);
    }
}
Output:
abc
a

Stream dropWhile(Predicate Interface) method

The dropWhile() method throw away all the values at the start until the predicate returns true. It returns, in case of ordered stream, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements matching the given predicate.
Example:
package net.javaguides.corejava.java9;

import java.util.stream.Stream;

/**
 * Demonstrates Java 9 Stream API Improvements
 * @author Ramesh Fadatare
 *
 */
public class Java9StreamAPIExample {
    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:
bcef
ef

Stream iterate() method

The iterate() method now has hasNext predicate as a parameter which stops the loop once hasNext predicate returns false.
Example:
package net.javaguides.corejava.java9;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Demonstrates Java 9 Stream API Improvements
 * 
 * @author Ramesh Fadatare
 *
 */
public class Java9StreamAPIExample {
    public static void main(String[] args) {
        List < Integer > numbers = Stream.iterate(1, i - > i <= 5, i - > i + 1)
            .collect(Collectors.toList());

        System.out.println(numbers);
    }
}
Output:
[1, 2, 3, 4, 5]

Stream ofNullable() method

In Java 9, the ofNullable() method lets you create a single-element stream that wraps a value if not null, or is an empty stream otherwise.
Example:
package net.javaguides.corejava.java9;

import java.util.stream.Stream;

/**
 * Demonstrates Java 9 Stream API Improvements
 * 
 * @author Ramesh Fadatare
 *
 */
public class Java9StreamAPIExample {
    public static void main(String[] args) {
        Stream < String > stream = Stream.ofNullable("Ramesh");
        System.out.println(stream.count());

        stream = Stream.ofNullable(null);
        System.out.println(stream.count());
    }
}
Output:
1
0

Related Java 9 Posts

Comments