How do you count elements in a list using Java streams?

count() is a Stream API terminal operation that returns the number of elements remaining in the stream after any intermediate operations like filter have been applied.

Key Points

Example

Given the list [3, 7, 2, 9, 4], filtering for values greater than 5 and calling count() returns 2, since only 7 and 9 satisfy the condition.

long count = numbers.stream()
        .filter(n -> n > 5)
        .count();

Interview Tip

A concise interview answer is

count() is a terminal operation that returns how many elements are left in the stream, typically after filtering with a condition. I use numbers.stream().filter(n -> n > 5).count() to count elements matching a predicate rather than looping manually.

Last updated: 11 November 2026

Previous question: How do you sort a list using Java streams?

Next question: How do you get distinct elements from a list using Java streams?

More Stream API Coding Questions - Level I interview questions

All Stream API Coding Questions - Level I questions