Quick revision
The short answer
Stream.reduce has three forms: reduce(op) returns an Optional because the stream may be empty, reduce(identity, op) returns a plain value that is the identity for an empty stream, and reduce(identity, accumulator, combiner) lets the result be a different type from the elements. The identity must really leave values unchanged and the operation must be associative, because a parallel stream splits the data into chunks, starts each chunk from the identity and then combines the chunk results in whatever grouping the split produced.
What to remember
In this answer, we saw the three forms of reduce: reduce(op) returning Optional, reduce(identity, op) returning T, and reduce(identity, accumulator, combiner) for a different result type. We saw that a parallel stream starts every chunk from the identity, so reduce(10, Integer::sum) adds 10 once per chunk and gives 50 instead of 20, and that chunk results are combined in whatever grouping the split produces, so only associative operations give stable answers. Finally, we saw that lists and other mutable containers belong in collect, not reduce.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Stream.reduce has three forms: reduce(op) returns an Optional because the stream may be empty, reduce(identity, op) returns a plain value that is the identity for an empty stream, and reduce(identity, accumulator, combiner) lets the result be a different type from the elements. The identity must really leave values unchanged and the operation must be associative, because a parallel stream splits the data into chunks, starts each chunk from the identity and then combines the chunk results in whatever grouping the split produced.
Why this question is asked
reduce looks like the simplest terminal operation, and in sequential code almost any lambda seems to work. The trouble starts when the same code runs on a parallel stream, or when someone uses reduce to build a list. Interviewers ask about the rules for identity and associativity because they reveal whether we understand what the stream library is allowed to do with our function, not only what it happens to do today. In this answer, we will recap what a reduction is, go through the three forms one by one, see exactly why a fake identity and a non-associative operation break under parallel execution, and finish with when to use collect instead.
1A Quick Recap of Reduction
A reduction combines all the elements of a stream into a single result: a sum, a maximum, a concatenated string. reduce does this by applying a binary operation, a function that takes two values and returns one, again and again. Sequentially it is just a running total: start with something, combine it with the first element, then with the second, and so on.
Two words from mathematics describe what the library needs from that operation. An identity is a value that changes nothing: 0 for addition, 1 for multiplication, the empty string for concatenation. Associative means the grouping does not matter: (a + b) + c equals a + (b + c). Subtraction is not associative, because (10 - 4) - 3 is 3 while 10 - (4 - 3) is 9.
2Form 1: reduce(op) and Its Optional
The first form takes only the operation. It uses the first element as the starting point and combines the rest into it.
Notice the return type. Without a starting value, an empty stream has nothing to return, so the method returns Optional<T> and makes the caller decide what an empty result means. This is the natural form for max and min, which have no sensible identity.
3Form 2: reduce(identity, op)
The second form takes an identity and the operation, and returns T directly. For an empty stream the result is simply the identity.
The element type and the result type are the same here, so the operation has the shape (T, T) -> T. That limits what we can compute: we cannot, for example, add up the lengths of strings without first mapping them to numbers.
4Form 3: reduce(identity, accumulator, combiner)
The third form allows a result type U that differs from the element type T. It needs two functions. The accumulator folds one element into a partial result, with the shape (U, T) -> U. The combiner merges two partial results, (U, U) -> U.
In a sequential stream the combiner is never called, which is exactly why mistakes in it go unnoticed. In a parallel stream each chunk is folded with the accumulator, and the combiner merges the chunk results. In practice, totalLength is usually clearer as mapToInt(String::length).sum(), and that is worth saying in an interview, but the three-argument form is the general shape that the other two are special cases of.
5Why the Identity Must Be a True Identity
A parallel stream splits its source into chunks, reduces each chunk on a different thread, and combines the results. Every chunk needs a starting value, and the only one the library has is the identity we gave it. So the identity is used once per chunk, not once per stream. That is only harmless if op(identity, x) == x for every x.
- Split the source into chunks[1, 2] and [3, 4], or one element each
- Each chunk starts from the identitywith 10 instead of 0, every chunk adds 10
- Accumulate inside each chunkon separate fork-join threads
- Combine chunk results pairwisegrouping decided by the split, not by us
- Return the final valuecorrect only if identity and associativity hold
5 steps in order. Play them, or step through them one at a time.
The identity appears once per chunk, and the grouping of the combines is up to the library.
The sequential stream adds 10 once: 10 + 1 + 2 + 3 + 4 = 20. On a machine with several cores the parallel stream splits four elements into four chunks, and each chunk starts from 10: (10 + 1) + (10 + 2) + (10 + 3) + (10 + 4) = 50. On a different number of cores the split is different, and so is the answer. The developer meant "start the total at 10", which is not an identity. The fix is to reduce with the real identity and add the offset afterwards: 10 + stream.reduce(0, Integer::sum).
6Why the Operation Must Be Associative
The second rule follows from the combining step. With chunks [1, 2] and [3, 4], the parallel stream computes (1 op 2) op (3 op 4). Sequentially it computes ((1 op 2) op 3) op 4. Those two are only guaranteed equal when op is associative. Addition, multiplication, max, min and string concatenation are associative. Subtraction and averaging are not.
For the three-argument form there is one more rule that ties the two functions together: combiner(u, accumulator(identity, t)) must equal accumulator(u, t). In words, folding an element in directly must give the same result as reducing it on its own and merging it in. If this does not hold, the sequential and parallel answers drift apart even when each function looks reasonable.
7Use collect for Lists and Other Mutable Containers
reduce is designed for immutable values: each step returns a new value. People sometimes use it to build a list, and that either copies the list at every step or, worse, mutates a shared list.
collect performs a mutable reduction: it creates a fresh container per chunk with a supplier, adds into it, and merges containers with a combiner. That is the right tool for lists, maps, StringBuilders and anything else we change in place.
8Follow-up questions
Why does reduce(op) return Optional but reduce(identity, op) does not?
Because with an identity there is always something to return. For an empty stream, reduce(0, Integer::sum) returns 0, the identity. Without an identity there is no element to start from and no meaningful default, so the API returns Optional.empty and forces the caller to choose.
Is floating-point addition associative?
Not exactly. (a + b) + c can differ from a + (b + c) in the last bits because each addition rounds. So reduce(0.0, Double::sum) on a parallel stream can give slightly different totals depending on the split. DoubleStream.sum and Collectors.summingDouble use compensated summation to keep the error small, but money should not be summed in double at all.
9Conclusion
In this answer, we saw the three forms of reduce: reduce(op) returning Optional, reduce(identity, op) returning T, and reduce(identity, accumulator, combiner) for a different result type. We saw that a parallel stream starts every chunk from the identity, so reduce(10, Integer::sum) adds 10 once per chunk and gives 50 instead of 20, and that chunk results are combined in whatever grouping the split produces, so only associative operations give stable answers. Finally, we saw that lists and other mutable containers belong in collect, not reduce.
Open with
reduce has three forms. reduce with just a binary operator starts from the first element and returns an Optional, because an empty stream has no result; that is the natural form for max or min. reduce with an identity and an operator returns T directly, and the identity is the answer for an empty stream.
Then, if they want more
And the three-argument form takes an identity, an accumulator of shape U and T to U, and a combiner of shape U and U to U, which lets the result type differ from the element type. The rules exist because of parallel streams.
The library splits the source into chunks, starts every chunk from the identity, and combines the chunk results in whatever grouping the split produced.
So the identity must really change nothing, op of identity and x equals x; if I write reduce(10, Integer::sum), sequentially I get 20 for one to four, but in parallel each chunk adds 10 and I can get 50. And the operation must be associative, because a op b op c may be grouped either way.
For the three-argument form, combiner of u and accumulator of identity and t must equal accumulator of u and t. And for building lists or maps I use collect, which gives each chunk its own container, not reduce.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.stream, package documentation, java.util.function, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain the three forms of Stream.reduce. Why must the identity value be a true identity...": https://scrivio-interview-library.netlify.app/java/streams/32b21d4052/