Java Stream count() Method with Examples

Ramesh Fadatare
3 min readSep 27, 2024

--

The count() method in Java is a part of the java.util.stream.Stream interface and it is used to count the number of elements in the stream. In this guide, we will learn how to use count() method in Java with practical examples and real-world use cases to better understand its functionality.

Learn Java from scratch: Learn Java Programming.

Complete Java Reference: Java API Documentation.

Table of Contents

  1. Introduction
  2. count() Method Syntax
  3. Examples
  4. Real-World Use Case
  5. Conclusion

Introduction

The Stream.count() method in Java is used to count the number of elements in a stream. It’s a terminal operation that returns the total count.

Typically, count() is used when you need to know how many elements are in a stream after processing or filtering.

Since it’s a simple method, count() is commonly used in scenarios where only the total number of elements matters, without collecting the actual data.

count() Method Syntax

The syntax for the count() method is as follows:

long count()

Parameters:

  • This method does not take any parameters.

Returns:

  • The count of elements in the stream as a long.

Throws:

  • This method does not throw any exceptions.

Examples

Basic Usage

To demonstrate the basic usage of count(), we will create a Stream and use count() to count the number of its elements.

import java.util.stream.Stream;

public class CountExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "cherry");

// Use count() to count the number of elements in the stream
long count = stream.count();

// Print the count
System.out.println("Count: " + count);
}
}

Output:

Count: 3

Using count() with Filtered Streams

This example shows how to use count() in combination with other stream operations, such as filtering.

import java.util.stream.Stream;

public class CountWithFilterExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "cherry", "apricot", "blueberry");

// Filter elements that start with 'a' and count them
long count = stream.filter(s -> s.startsWith("a")).count();

// Print the count of filtered elements
System.out.println("Count of elements starting with 'a': " + count);
}
}

Output:

Count of elements starting with 'a': 2

Real-World Use Case: Counting Employees Above a Certain Age

In real-world applications, the count() method can be used to count the number of employees above a certain age from a stream of employee objects.

import java.util.stream.Stream;

public class EmployeeCountExample {
static class Employee {
String name;
int age;

Employee(String name, int age) {
this.name = name;
this.age = age;
}

int getAge() {
return age;
}
}

public static void main(String[] args) {
Stream<Employee> employeeStream = Stream.of(
new Employee("Alice", 30),
new Employee("Bob", 25),
new Employee("Charlie", 35),
new Employee("David", 28)
);

// Count employees above the age of 30
long count = employeeStream.filter(employee -> employee.getAge() > 30).count();

// Print the count of employees above the age of 30
System.out.println("Count of employees above 30: " + count);
}
}

Output:

Count of employees above 30: 1

Conclusion

The Stream.count() method is used to count the number of elements in a stream. This method is particularly useful for determining the size of the stream after applying various intermediate operations.

By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, obtaining counts as needed.

Original Post:

https://www.rameshfadatare.com/java-stream/java-stream-count-method/

--

--

No responses yet