1Z0-809 · Question #219
Which two methods from the java.util.stream.Stream interface perform a reduction operation? (Choose two.)
The correct answer is A. count () B. collect (). count() returns a single long value representing the total number of elements, making it a terminal reduction that collapses the entire stream into one result. collect() is a mutable reduction that accumulates stream elements into a container such as a List or Map - it also…
Question
Options
- Acount ()
- Bcollect ()
- Cdistinct ()
- Dpeek ()
How the community answered
(21 responses)- A76% (16)
- C14% (3)
- D10% (2)
Explanation
count() returns a single long value representing the total number of elements, making it a terminal reduction that collapses the entire stream into one result. collect() is a mutable reduction that accumulates stream elements into a container such as a List or Map - it also terminates the stream and produces a single aggregated result.
distinct() and peek() are both intermediate operations - they return a new Stream, meaning they transform the pipeline but never produce a final result on their own. distinct() filters duplicates, and peek() is a debugging hook that lets you inspect elements as they pass through without consuming them.
Memory tip: If a Stream method returns a Stream, it's intermediate - not a reduction. Reductions always terminate the pipeline and hand back a concrete value, collection, or OptionalX type.
Community Discussion
No community discussion yet for this question.