Java Streams Counting Method with Examples



In this article, we will learn how to count the number of elements in a stream using the counting() method in Java Streams. Java Streams provide an efficient way to process data collections, and the Collectors.counting() method is useful for counting the number of elements in a stream.

The counting() method is a static method of the Collectors class, which is part of the java.util.stream package. It returns a long value representing the number of elements in the stream.

Counting Number of Elements in the Stream

The following are the steps to count the number of elements in the stream:

  • Import the necessary classes from java.util and java.util.stream packages.
  • Create a stream of elements (either strings or integers).
  • Apply the collect (Collectors.counting()) method to count the number of elements in the stream.
  • Display the count of elements.

Example

The following is an example of counting the number of elements in a stream -

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) {
      Stream<String> stream = Stream.of("Kevin", "Jofra","Tom", "Chris", "Liam");
      // count
      long count = stream.collect(Collectors.counting());
      System.out.println("Number of elements in the stream = "+count);
   }
}

The output of the above code will be -

Number of elements in the stream = 5

Example: Counting Elements in an Integer Stream

The following is another example of counting the number of integer elements in a stream

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) {
      Stream<Integer> stream = Stream.of(5, 10, 20, 40, 80, 160);
      // count
      long count = stream.collect(Collectors.counting());
      System.out.println("Number of elements in the stream = "+count);
   }
}

The output of the above code will be:

Number of elements in the stream = 6

Counting elements in a Stream of Custom Objects

The counting() method can also be applied to a stream of custom objects. In this case, we will create a class called Person with fields for name and age. We will create a list of Person objects and use the counting() method to count the number of elements in the stream.

  • The list.stream() creates a sequential stream from the list.
  • The collect(Collectors.counting()) method counts the number of elements in the stream.
  • The getName() method retrieves the name of the person.
  • The getAge() method retrieves the age of the person.

Example

Below is an example of implementing the counting() method in Java :

import java.util.*;
class Person {
   String name;
   int age;
   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }
   public String getName() {
      return name;
   }
   public int getAge() {
      return age;
   }
}
public class Demo {
   public static void main(String[] args) {
      List<Person> list = Arrays.asList(new Person("Kevin", 25), new Person("Jofra", 30), new Person("Tom", 35));
      // count
      long count = list.stream().count();
      System.out.println("Number of elements in the stream = "+count);
   }
}

The output of the above code will be -

Number of elements in the stream = 3
Updated on: 2025-05-08T18:03:47+05:30

369 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements