I'm learning Java 8, in Reduction operations part in java.util.stream
's package summary, it says:
More formally, the identity value must be an identity for the combiner function. This means that for all u, combiner.apply(identity, u) is equal to u. Additionally, the combiner function must be associative and must be compatible with the accumulator function: for all u and t, combiner.apply(u, accumulator.apply(identity, t)) must be equals() to accumulator.apply(u, t).
I don't understand why the identity value must be an identity for the combiner function. I think "combiner function must be associative and must be compatible" is enough to produce the same result either the stream is serial or parallel.
For example, I have a stream that has four elements e1, e2, e3, e4
. If it's a serial stream, the result is identity
ac e1
ac e2
ac e3
ac e4
(ac means accumulator function). If it's a parallel stream, the four elements can be split into two parts, [e1, e2]
and [e3, e4]
, so the result is (identity
ac e1
ac e2
) co (identity
ac e3
ac e4
).
If given "the combiner function is associative and is compatible with the accumulator function", we can infer that "identity
ac e1
ac e2
ac e3
ac e4
" is equal to "(identity
ac e1
ac e2
) co (identity
ac e3
ac e4
)":
identity ac e1 ac e2 ac e3 ac e4
= (identity ac e1 ac e2 ac e3) co (identity ac e4) // because of compatibility
= (identity ac e1 ac e2) co (identity ac e3) co (identity ac e4) // because of compatibility
= (identity ac e1 ac e2) co ((identity ac e3) co (identity ac e4)) // because of associative property
= (identity ac e1 ac e2) co ((identity ac e3) ac e4) // because of compatibility
= (identity ac e1 ac e2) co (identity ac e3 ac e4)
So, why must the identity value be an identity for the combiner function?
Related questions: