Stream empty() in Java with Examples

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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream empty()
import java.util.*;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an empty Stream
        Stream<String> stream = Stream.empty();
  
        // Displaying elements in Stream
        stream.forEach(System.out::println);
    }
}

chevron_right


Output :

No Output


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.