Stream.anyMatch()
Checks whether at least one element matches a condition.
Available since: Java 8
Syntax
boolean anyMatch(Predicate<? super T> predicate)Parameters
| Parameter | Type | Description |
|---|---|---|
| predicate | Predicate<? super T> | The condition to check. |
Return value
boolean`true` if at least one element matches.
Examples
Does an order contain an expensive item:
void main() {
List<Double> prices = List.of(9.99, 120.0, 45.5);
IO.println(prices.stream().anyMatch(p -> p > 100));
IO.println(prices.stream().anyMatch(p -> p > 500));
}Output:
true
falseNotes
It stops at the first matching element. On an empty stream it returns false.
See also
- Stream.allMatch() — Checks whether all elements match a condition.
- Stream.noneMatch() — Checks that no element matches a condition.
- List.contains() — Checks whether a list contains an element.
- Stream.findFirst() — Returns the first stream element as an `Optional`.
Related recipes
In the tutorial
- Stream operations — The main Stream API operations — filter, map, sorted, distinct, limit, skip, count, reduce, anyMatch, findFirst and numeric streams.