LongStream count() method in Java


The count() method of the LongStream class in Java is used to return the count of elements in this stream.

The syntax is as follows.

long count()

To use the LongStream class in Java, import the following package.

import java.util.stream.LongStream;

Create a LongStream and add some elements.

LongStream longStream = LongStream.of(50L, 30L, 80L, 40L, 15L, 60L);

Now, get the count of elements.

longStream.count()

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

Example

 Live Demo

import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(50L, 30L, 80L, 40L, 15L, 60L);
      System.out.println("The number of elements in the stream = "+longStream.count());
   }
}

Output

The number of elements in the stream = 6

Updated on: 30-Jul-2019

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements