Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

Stream.anyMatch()

Checks whether at least one element matches a condition.

Available since: Java 8

Syntax

boolean anyMatch(Predicate<? super T> predicate)

Parameters

ParameterTypeDescription
predicatePredicate<? 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
false

Notes

It stops at the first matching element. On an empty stream it returns false.

See also

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.

Thanks for reading!

Donate to the site — we work hard for you.