Close

Java 8 Streams - Stream.empty Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.Stream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.StreamStreamLogicBig

Method:

static <T> Stream<T> empty()

Returns an empty sequential Stream.


Examples


An empty stream might be useful to avoid null pointer exceptions while callings methods with stream parameters.

package com.logicbig.example.stream;

import java.util.stream.Stream;

public class EmptyExample {

public static void main(String... args) {
Stream<Object> myStream = Stream.empty();
someMethod(myStream);
}

public static void someMethod(Stream<?> stream) {
stream.forEach(System.out::println);
}
}

Output





See Also