Saturday, August 30, 2025
HomeLanguagesJavaStream empty() in Java with Examples

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 :




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


Output :

No Output
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11839 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6703 POSTS0 COMMENTS