Java Stream reduce with Identity vs Accumulator

Answer Correct answer: D — The code fragment int sum = listOfNumbers.parallelStream().reduce(5, Integer::sum); returns a different value because the identity 5 is added to each sub-stream's result in a parallel execution.

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?

  1. int sum = listOfNumbers.stream().reduce(0, Integer::sum) + 5;
  2. int sum = listOfNumbers.parallelStream().reduce(0, Integer::sum) + 5 ;
  3. int sum = listOfNumbers.parallelStream().reduce((m, n) -> m + n).orElse(5) + 5;
  4. int sum = listOfNumbers.parallelStream().reduce(5, Integer::sum); Correct Answer
  5. int sum = listOfNumbers.stream().reduce(5, (a, b) -> a+ b);

Community Votes

D
100%

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)

SrinivasJasti 👍 1 Selected: D
Non-determinism in parallel streams, the elements are partitioned into smaller sub-streams, and the reduction is performed on each sub-stream independently. it's possible that the 5 (initial value provided to reduce()) is added to some elements before they are added to others.
xplorerpj 👍 1
D is correct answer
Samps 👍 1
// D. We need to be careful while using parallelStream()
c6437d5 👍 2 Selected: D
D tested correct
james2033 👍 2 Selected: D
// D. int sum = listOfNumbers.parallelStream().reduce(5, Integer::sum); // (1 + 5) + (2 + 5) + (3 + 5) + (4 + 5) + (5 + 5) + (6 + 5) + (7 + 5) + (8 + 5) + (9 + 5) + (10 + 5) = 105 System.out.println(">>> sum = " + sum); // >>> sum = 105 A, B, C, E return sum = 60.

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

Option D uses the three-argument form reduce(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 identity 5 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.

More 1Z0-829 FAQ →

Related Analysis

← Back to 1Z0-829 Study Guide