posted 6 years ago
A. (a,b) -> (a-b)
B. (a,b) -> 5
C. (a,b) -> i++
D. None of the above are appropriate.
The answer in the book is B, because it satisfies the two requirements - being associative and stateless. I totally agree with this answer if the reduce had just two arguments - identity and accumulator.
I am confused here , because there is a combiner also present in the reduce method which is s1+s2. Now, combiner has the contract that should return u, which is satisfied here as identity is 0. There is one more requirement : should be equivalent to
Suppose u and t both here are 5 as accumulator returns 5 for any two values in stream. so, combiner.apply(5, 5) is 10 whereas accumulator.apply(5,5) would return 5. So, as a whole , to me the answer D looks closer.
Please clarify if my understanding is not right.