Java 25 (LTS) reference · Stream API
Java Stream anyMatch() Method
Return true if any element matches a condition, stopping at the first match. allMatch() and noneMatch() are similar.
Java 25 (LTS) Since Java 8
Syntax
stream.anyMatch(predicate)
Parameters
| Parameter | Description |
|---|---|
predicatePredicate<T> |
The condition. |
Return value
true or false.
Examples
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) {
List<Integer> ages = List.of(15, 22, 31);
System.out.println(ages.stream().anyMatch(a -> a < 18));
System.out.println(ages.stream().allMatch(a -> a < 40));
System.out.println(ages.stream().noneMatch(a -> a > 100));
}
}
Output
true true true
Run this on your computer — see “Get Started” for setup.
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) {
System.out.println(Stream.<Integer>empty().allMatch(n -> n > 1000)); // vacuously true
}
}
Output
true
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| Java Java 8 | Added |
Related
stream.filter()· Keep only the elements that match a condition. Intermediate and lazy: nothing runs until a terminal operation.stream.count()· Return the number of elements in the stream (a long). A terminal operation.- Java Streams · tutorial