Easy Tutorial
❮ Method Instanceof String Last Occurance ❯

Java 9 Improved Stream API

Java 9 New Features

Java 9's improved Stream API introduces several convenient methods that make stream processing easier and allow for writing complex queries using collectors.

Java 9 adds several new methods to Stream: dropWhile, takeWhile, ofNullable, and an overloaded method for iterate.

takeWhile Method

Syntax

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

The takeWhile() method takes a predicate as an argument and returns a subset of the given Stream until the predicate first returns false. If the first value does not satisfy the predicate condition, it returns an empty Stream.

In an ordered Stream, takeWhile returns as many elements as possible from the beginning; in an unordered Stream, takeWhile returns a subset of elements from the beginning that satisfy the Predicate.

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);      
   } 
}

The above example stops looping and outputs when it encounters an empty string, resulting in:

abc

dropWhile Method

Syntax

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

The dropWhile method is the opposite of takeWhile, taking a predicate as an argument and returning a subset of the given Stream from the point where the predicate first returns false.

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);
   } 
}

The above example starts looping and outputs from the point where it encounters an empty string, resulting in:

ef

iterate Method

Syntax

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

This method allows creating a sequential (possibly infinite) stream from an initial seed value and iteratively applying the specified next method. The iteration stops when the specified hasNext predicate returns false.

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);
   } 
}

The output is:

3
6
9

ofNullable Method

Syntax

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

The ofNullable method prevents NullPointerExceptions by checking the stream to avoid null values.

If the specified element is non-null, it creates a single-element stream; if the element is null, it returns an empty stream.

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);
   } 
}

The output is:

1
0
System.out.println(count);
} 
}

The execution output is:

1
0

New Features in Java 9

❮ Method Instanceof String Last Occurance ❯