Streams: anyMatch, allMatch and noneMatch stop early

These short-circuiting predicates return as soon as the answer is known, so they never scan the whole stream unnecessarily.

Code
var nums = List.of(2, 4, 6, 7, 8);

System.out.println("anyMatch(odd)   = " + nums.stream().anyMatch(n -> n % 2 != 0));
System.out.println("allMatch(even)  = " + nums.stream().allMatch(n -> n % 2 == 0));
System.out.println("noneMatch(>100) = " + nums.stream().noneMatch(n -> n > 100));

System.out.println("allMatch on empty is true: " + Stream.<Integer>of().allMatch(n -> n > 5));
Output
anyMatch(odd)   = true
allMatch(even)  = false
noneMatch(>100) = true
allMatch on empty is true: true
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-30