IntStream count() method in Java



The count() method of the IntStream class in Java returns the count of elements in this stream

The syntax is as follows

long count()

First, create an IntStream and add some elements

IntStream intStream = IntStream.of(30, 13, 67, 56, 89, 99, 76, 56);

Now, get the count of elements of the stream using the count() method

long num = intStream.count();

The following is an example to implement IntStream count() method in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(30, 13, 67, 56, 89, 99, 76, 56);
      long num = intStream.count();
      System.out.println("Count of elements in the stream: "+num);
   }
}

Output

Count of elements in the stream: 8

Advertisements