Stream empty() in Java with Examples
Last Updated :
06 Dec, 2018
Stream empty() creates an empty sequential Stream.
Syntax :
static <T> Stream<T> empty()
Parameters :
- T : The type of stream elements.
- Stream : A sequence of objects that supports various methods which can be pipelined to produce the desired result.
Return Value : Stream empty() returns an empty sequential stream.
Note : An empty stream might be useful to avoid null pointer exceptions while callings methods with stream parameters.
Example :
import java.util.*;
import java.util.stream.Stream;
class GFG {
public static void main(String[] args)
{
Stream<String> stream = Stream.empty();
stream.forEach(System.out::println);
}
}
|
Output :
No Output
Share your thoughts in the comments
Please Login to comment...