Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 181

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?

    Correct Answer: B

    To check if there is any item in the list with a count below zero, the most appropriate method is `items.stream().anyMatch(i -> i.count < 0)`. This method returns a boolean value, true if any item matches the condition, and false otherwise. The method `allMatch` checks if all items match the condition, which is not what is required here. The `filter` methods combined with `findAny` or `findFirst` would return an `Optional`, not a boolean, and thus would not compile within an `if` statement checking for a Boolean condition.

Discussion
d7bb0b2Option: B

B 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.

d7bb0b2Option: B

B 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.