CKTIS / Java
Stream.anyMatch: practical reference
Stream.anyMatch: practical reference: Stream.anyMatch(predicate) is a short-circuiting terminal operation that returns true when at least one element satisfies the predicate.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
Stream.anyMatch(...)Stream.anyMatch(predicate) is a short-circuiting terminal operation that returns true when at least one element satisfies the predicate.
Use it to test whether a match exists instead of filter(...).findAny().isPresent() or counting every match.
After the first true, remaining elements need not be evaluated; in parallel execution the predicate may run concurrently and out of order.
An empty stream returns false without invoking the predicate; a null predicate throws NullPointerException, and the operation consumes the stream.
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(1, 3, 4).anyMatch(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.