Which Stream Method Finds an Item With count Below Zero?
Short answer
B AnyMatch on the items stream returns true when any item's count is below zero, which is exactly the check required.
Question
- 
Key Takeaways
It tests the difference between boolean-returning match operations (anyMatch/allMatch) and Optional-returning search operations (findAny/findFirst) — the trap is writing filter(...).findAny() directly inside an if, which does not compile.
This 1Z0-819 question asks which Stream pipeline correctly tests whether the items list contains at least one element whose count field is below zero. The answer is anyMatch, because it is the boolean-returning, short-circuiting operation that expresses an existential check.
Common mistake: The most common wrong pick is filter(i -> i.count < 0).findAny(), because it does locate a matching item; however it returns an Optional, not a boolean, so it cannot be used as an if condition without isPresent().
Expert Analysis
Why the Answer Is Correct
The requirement is existential: does the items list contain at least one item whose count is below zero? Stream.anyMatch(Predicate) returns a boolean and short-circuits as soon as the first matching element is found, which is precisely that semantic.
items.stream.anyMatch(i -> i.count < 0) evaluates true if a single negative count exists and false if none do, so it drops straight into the if (...) at line 1 without any unwrapping or conversion.
Because it short-circuits, anyMatch also stops the pipeline at the first match, so it never examines more elements than necessary — the idiomatic and efficient way to answer a "contains any item for which..." question in Java 8+.
Why the Other Options Are Wrong
Option A uses allMatch, which asks a different question: whether every item has count below zero. An empty list would even return true, and a list with one non-negative count returns false — the opposite of the intended check.
Options C and D use filter(i -> i.count < 0).findAny and findFirst. Both terminal operations return Optional<Item>, not a boolean, so the resulting expression cannot be used as the condition of an if statement and the fragment fails to compile.
To make C or D valid you would have to add .isPresent (or a boolean unwrap), which the question's fragments do not do; additionally findFirst imposes encounter-order semantics that are irrelevant to a simple existence test.
Community Comment Notes
ASPushkin listed the relevant signatures and contrasted them directly: Optional<T> findAny and Optional<T> findFirst versus boolean anyMatch(...), concluding that anyMatch is the answer. That signature contrast is exactly why C and D are rejected.
d7bb0b2 makes the same point more bluntly, stating that "C and D retrive an object no a boolean value so if not compile", and adds that allMatch checks whether all elements meet the condition and short-circuits — which explains why A changes the meaning of the test rather than just its style.
The unanimous vote for B is consistent with the API contracts: only a boolean-returning match operation can appear directly inside the if at line 1.
Official Reference
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/Stream.html
This link opens the official vendor documentation in a new tab.
Exam Strategy
When a question asks whether a collection "contains" or "contains any" element satisfying a condition, reach for anyMatch; when it asks whether all/none satisfy it, use allMatch/noneMatch. Reserve findAny/findFirst for when you actually need the element, and remember they return Optional, so they never belong directly in an if condition.
Frequently Asked Questions
Why doesn't filter(...).findAny() compile inside an if statement?
When should allMatch be used instead of anyMatch on items?
Community Votes
100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 3 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 3
-
ASPushkin 👍 1answer: D Optional<T> findAny() Optional<T> findFirst() boolean AllMatch(Predicate<? super T> predicate) boolean anyMatch(Predicate<? super T> predicate) answer: anyMatch
-
d7bb0b2 👍 1B is correct: allmatch check for all element match with the condition and is shortcircuit. C and D retrive an element no a boolean value so if not compile.
-
d7bb0b2 👍 1 Answered BB is correct: allmatch check for all element match with the condition and is shortcircuit. C and D retrive an object no a boolean value so if not compile.
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.