Stream anyMatch allMatch noneMatch Short Circuit

anyMatch, allMatch, and noneMatch stop consuming the stream at the first decisive element, and an empty stream answers true for all and none, false for any.

JavaData Fetching

Code

snippet
import java.util.List;

public class MatchShortCircuit {

    public static void main(String[] args) {
        List<Integer> balances = List.of(120, -5, 300, 40);

        // anyMatch:找到第一个满足的元素就短路,不再消费剩余元素
        System.out.println("any overdrawn: " + balances.stream().anyMatch(b -> b < 0));

        // allMatch:遇到第一个不满足的就短路返回 false
        System.out.println("all positive: " + balances.stream().allMatch(b -> b > 0));

        // noneMatch:找到违例即短路返回 false
        System.out.println("none over 500: " + balances.stream().noneMatch(b -> b > 500));

        // 空流的结论:allMatch 与 noneMatch 是 true(没有反例),anyMatch 是 false
        System.out.println("empty all: " + List.<Integer>of().stream().allMatch(b -> b > 0));
        System.out.println("empty any: " + List.<Integer>of().stream().anyMatch(b -> b > 0));
    }
}

Inside the snippet

Stream anyMatch allMatch noneMatch Short Circuit reduces to the class MatchShortCircuit and the function main.

The transformation runs as a stream pipeline — source, intermediate operations, terminal collector — so each stage stays independently readable.

Watch out for

  • There is no dependency tree here — the platform is the whole dependency.

Putting it to use

The file announces its steps in the output: any overdrawn:, all positive:, none over 500:.

Core lines

List<Integer> balances = List.of(120, -5, 300, 40);

FAQ

What does Stream anyMatch allMatch noneMatch Short Circuit print when run?
The script runs standalone; its 5 labeled outputs include `any overdrawn:`, `all positive:`, `none over 500:`.