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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

stream.anyMatch(predicate)

Parameters

ParameterDescription
predicate
Predicate<T>
The condition.

Return value

true or false.

Examples

any, all and none
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.

Empty streams
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

VersionChange
Java Java 8Added

Was this page helpful?