CKTIS / Java
Stream.count: practical reference
Stream.count: practical reference: Stream.count is a terminal reduction returning as a long the number of elements reaching that point in the pipeline.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
Stream.count(...)Stream.count is a terminal reduction returning as a long the number of elements reaching that point in the pipeline.
Use it when only cardinality is needed after filter, limit, or other operations that change which elements remain.
An implementation may derive the size without traversing the source and elide stages such as peek when they cannot affect the count; do not rely on their side effects.
The operation consumes the stream; an unbounded infinite pipeline may never finish, and an empty stream yields 0L.
The example exercises the documented element in a small deterministic program and compares exact standard output.
Examples
public class Main {
public static void main(String[] args) throws Exception {
long count = java.util.stream.Stream.of("a", "b", "c").count();
System.out.println(count);
}
}
Result
3
The example exercises the documented element in a small deterministic program and compares exact standard output.