Java Stream reduce with Identity vs Accumulator
Given the code fragment: List<Integer> listOfNumbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Which code fragment returns different values?
Community Votes
100% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
The question tests the difference between single-argument and three-argument reduce, specifically highlighting the trap where the identity element is added to every intermediate result in a parallel execution.
This question tests the behavior of the three-argument reduce method in Java Streams. It establishes that providing an identity value to a parallel stream's accumulator function can lead to incorrect results if the operation is not associative.
Learners often assume all reduce operations are equivalent or forget that the identity parameter in parallel streams acts as an initial value for each sub-task, effectively adding it multiple times.
Community Discussion (5 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
Option D uses the three-argument formreduce(identity, accumulator, combiner). In a parallel stream, the stream is partitioned into chunks. The accumulator function (a, b) -> a + b (via Integer::sum) is applied to each chunk, initialized with the identity 5. This means 5 is added to the sum of each chunk. Since there are multiple chunks, 5 is added multiple times before the results are combined, leading to an inflated sum (105 instead of 60). The other options either use no identity (C), use sequential streams where identity is only used once (A, B, E), or correctly handle the reduction without redundant identity addition.Why the Other Options Are Wrong
Options A, B, and E perform standard summation. A and E use sequential streams, so the identity5 is added exactly once at the start. B uses parallelStream but relies on the two-argument reduce which returns an Optional, then adds 5 outside, ensuring 5 is added only once. Option C uses the two-argument reduce on a parallel stream; while it might have non-deterministic ordering, it doesn't add the identity multiple times in the same erroneous way as D, and its final value is still 60 (plus the external +5).Community Comment Notes
Community consensus strongly supports D. As user james2033 noted, the calculation shows the identity being added to each element in a way that inflates the total: "(1 + 5) +..." resulting in 105. User SrinivasJasti explained the underlying mechanism: "Non-determinism in parallel streams... it's possible that the 5... is added to some elements before they are added to others," correctly identifying the root cause of the discrepancy.Exam Strategy
Always distinguish between the single-argument reduce and the three-argument reduce. When using parallel streams with an identity, ensure the accumulator function is associative and commutative with the identity, or avoid using the identity parameter to prevent double-counting.
Frequently Asked Questions
Why does parallelStream().reduce(5, ...) add 5 multiple times?
In parallel streams, the accumulator is applied to partitions. Each partition starts with the identity, so 5 is added per partition before combining results.
Is Integer::sum safe for parallel reduce?
Yes, addition is associative. However, using the identity argument incorrectly causes it to be added to each partition's initial state, inflating the total.