Home  >  Java 8

Java Stream count()

By Arvind Rai, May 25, 2020
This page will walk through Stream.count() method example. The count() method returns the count of elements in this stream. The count() method is the special case of stream reduction.
Find the count() method declaration from Java doc.
long count() 
Returns:
The count() returns the count of elements in this stream.

The count() is the stream terminal operation. Stream operations are divided into intermediate and terminal operations and are combined to form stream pipelines. Intermediate operations are lazy operations such as filter() method and they actually does not perform any filtering instead they create a new stream and on traversal of initial stream they contain only those elements which matches the given predicate. The traversal of pipeline stream takes place only when the terminal operation such as count() is executed.

Example-1: Find the list of numbers.
List<Integer> numList = Arrays.asList(42, 44, 43, 41); 
The count() method returns the count of elements in the stream.
long c = numList.stream().count(); 
The value of c will be 4.
Now let us use the filter method with stream and then count the elements.
long c = numList.stream().filter(e -> e % 2 == 0).count(); 
The value of c will be 2.

The count() is the special case of stream reduction. Find the code to get count without using count() method.
long c = numList.stream().mapToLong(e -> 1L).sum(); 
The value of c will be 4.

Example-2: Find the list of names.
List<String> strList = Arrays.asList("Mohit", "Nilesh", "Mahesh"); 
Let us use the count() on the stream of the list.
long c = strList.stream().count(); 
The value of c will be 3.
Now use the filter on the stream of string and count it.
long c = strList.stream().filter(e -> e.startsWith("M")).count(); 
The value of c will be 2.

As we know that count() is the special case of stream reduction. Let us find the count without using count() method.
long c = strList.stream().filter(e -> e.startsWith("M")).mapToLong(e -> 1L).sum(); 
The value of c will be 2.

Example-3: In this example we have a list of objects. We will filter the stream of list according to given condition and then count the elements.
CountDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class CountDemo {
  public static void main(String[] args) {
	List<User> users = Arrays.asList(
		new User("Vikas", 30),
		new User("Krishna", 29),
		new User("Virat", 30),
		new User("Mahesh", 30)
	);
	long c1 = users.stream().filter(u -> u.getUserName().startsWith("V")).count();
        System.out.println(c1); //Output: 2
    
	long c2 = users.stream().filter(u -> u.getAge() == 30).count();
        System.out.println(c2); //Output: 3
  }
}
class User {
  private String userName;
  private int age;
  public User(String userName, int age) {
	this.userName = userName;
	this.age = age;
  }
  //Sets and Gets
} 
Output
2
3 

Reference

Java doc: Stream
POSTED BY
ARVIND RAI
ARVIND RAI








©2020 concretepage.com | Privacy Policy | Contact Us