On page 374 of the OCP 8 Programmer II, the authors list the requirements needed to use reduce() on parallel streams. However, I don't understand a thing about the explanations given after each bullet point. I know how the reduce() method works, I just don't understand the rules.
■ The identity must be defined such that for all elements in the stream p ,
combiner.apply(identity, p) is equal to p .
■ The accumulator operator op must be associative and stateless such that (a op b) op c
is equal to a op (b op c) .
■ The combiner operator must also be associative and stateless and compatible with the
identity, such that for all p and t combiner
Some of these rules matter more for parallel streams.
This code break the rule about identity and outputs something like -w-o-l-f. Why? Because the identity isn't really an identity. You should be able to apply an identity as many times as you want without the answer changing. That's why empty string is a far better identity value!
Now let's break the other rules. What's the output of this
The correct answer is that we don't know. IT depends on the order the stream elements are processed. Not good!