Which Stream Method Finds an Item With count Below Zero?
Given: and the code fragment: You want to examine the items list if it contains an item for which the variable count is below zero. Which code fragment at line 1 will accomplish this? -
- 
Community Votes
100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
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.
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().
Community Discussion (3 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
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
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?
findAny() and findFirst() return Optional<Item>, not a boolean, so they cannot be used directly as an if condition; you would need to append isPresent().
When should allMatch be used instead of anyMatch on items?
allMatch returns true only if every item satisfies the predicate, so it answers a different question and is wrong when you just need to detect one item with count below zero.