CKTIS / Java
Stream.allMatch: practical reference
Stream.allMatch: practical reference: Stream.allMatch(predicate) is a short-circuiting terminal operation that returns true when every encountered element satisfies the predicate.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
Stream.allMatch(...)Stream.allMatch(predicate) is a short-circuiting terminal operation that returns true when every encountered element satisfies the predicate.
Use it for universal validation of a data set without materializing results or writing a manual loop.
After the first false, further elements need not be tested; in a parallel stream, predicate call count and order cannot support side effects.
An empty stream returns true without evaluating the predicate; a null predicate causes NullPointerException.
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 {
boolean result = java.util.stream.Stream.of(2, 4, 6).allMatch(n -> n % 2 == 0);
System.out.println(result);
}
}
Result
true
The example exercises the documented element in a small deterministic program and compares exact standard output.