Java Stream reduce with Identity and Combiner
Given: What is the result? - 
Community Votes
70% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
The key insight is understanding that in parallel streams, the combiner function (String::concat) combines the results of different partitions, while the accumulator processes elements within a partition.
This question tests the behavior of the parallelStream reduce method when using an identity, accumulator, and combiner. The correct output is determined by how the combiner merges partial results from multiple threads.
Many candidates incorrectly choose compilation failure because they believe the variable names 'n' and 'm' or the numeric literal '1' cause issues, but these are valid Java identifiers and syntax.
Community Discussion (9 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
The code usesreduce with three arguments: identity "w", accumulator (n,m) -> n+m, and combiner String::concat. In a parallel stream, elements are split into partitions. Each partition starts with the identity "w" and applies the accumulator to its elements. For the list ["Table", "Chair"], one partition might process "Table" resulting in "wTable", and another might process "Chair" resulting in "wChair". The combiner then merges these two results: "wTable" + "wChair" = "wTablewChair". The second method attach2 uses a two-argument reduce which returns an Optional<String>. Since the stream is not empty, it contains "TableChair". Thus, the output is "wTablewChair TableChair".Why the Other Options Are Wrong
Option A is incorrect because the code compiles successfully; there are no type mismatches or syntax errors. Option D is incorrect because no exception is thrown during execution. Options C and E are incorrect because they do not reflect the actual string concatenation order produced by the parallel reduction logic.Community Comment Notes
Several users confirmed B is correct after testing in compilers, noting the specific output "wTablewChair TableChair". Some users initially thought A was correct due to confusion about variable types or lambda syntax, but verified that the code runs fine. One user explained the role of the combiner merging partial results like "wTable" and "wChair".Exam Strategy
When analyzing parallel stream reduce operations, always consider how the combiner function merges partial results from different threads. Remember that the identity element is used for each thread's initial state.
Frequently Asked Questions
Why does the first reduce produce "wTablewChair"?
In parallel streams, the combiner merges partial results. Here, it combines "wTable" and "wChair".
Is the second reduce result deterministic?
Yes, since it uses a two-argument reduce without a combiner on a small list, the order of addition doesn't change the final string for this specific case.