Java - Stream Empty Streams

Introduction

An empty stream is a stream with no elements.

Stream interface empty() static method creates an empty sequential stream.

// Creates an empty stream of strings
Stream<String> stream = Stream.empty();

IntStream, LongStream, and DoubleStream interfaces contain an empty() static method to create an empty stream of primitive types.

// Creates an empty stream of integers
IntStream numbers = IntStream.empty();

Demo

import java.util.stream.IntStream;

public class Main {
  public static void main(String[] args) {
    // Creates an empty stream of integers
    IntStream numbers = IntStream.empty();
    System.out.println(numbers.count());

  }//  w w w.  j  a  v  a  2  s .  c o  m
}

Result