Saturday, July 27, 2019

Java 8 IntStream empty() Method Example

1. Introduction


In this tutorial, We'll learn about Java 8 Instream API empty() method example programs.

empty() method is used to create a new empty Instream. The returned stream is a sequential stream. That means all the elements of Stream are processed one by one the order they appear in it.



Java8_IntStream_empty


Java 8 introduced an Optional class which also has Optional.empty() method to create an empty Optional instance.

2. IntStream empty() syntax:

static IntStream empty()

This is a static method and can be called directly with the class name. Empty() method returns a new IntStream instance.

Java8_IntStream_Class

3. IntStream empty() method Example:


This program is to check the created stream size is 0.

package com.java.w3schools.blog.java8.stream.intstream;

import java.util.stream.IntStream;

/**
 * 
 * Example program to create an empty intstream.
 * 
 * @author venkatesh nukala
 *
 */
public class IntStreamEmptyExample {

 public static void main(String[] args) {

  IntStream emptyIntStrweam = IntStream.empty();
  long count = emptyIntStrweam.count();
  System.out.println("Created instream size: " + count);

 }

}

Output:

Created instream size: 0

4. Conclusion


In this article, We've seen how to create an empty IntStream using empty() method. The created stream holds Integer values.

empty() method is staic mehtod which can be accessed as IntStream.empty().

This method returns a brand new empty IntStream instance.

As usual, the example program shown is over GitHub.


No comments:

Post a Comment