Empty Streams

Description

An empty stream has no elements.

Example

We can use empty() static method from Stream interface to create an empty sequential stream.


import java.util.stream.Stream;
/* w w w . j  a v  a 2  s.co m*/
public class Main {
  public static void main(String[] args) {
    Stream<String> stream  = Stream.empty();
    stream.forEach(System.out::println);
  }
}

Note

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

The following code creates an empty stream of integers.

IntStream numbers = IntStream.empty();




















Home »
  Java Streams »
    Tutorial »




Java Streams Tutorial