Why is a combiner needed for reduce method that converts type in java 8

后端 未结 4 2108
野的像风
野的像风 2020-11-27 09:42

I\'m having trouble fully understanding the role that the combiner fulfils in Streams reduce method.

For example, the following code doesn

4条回答
  •  佛祖请我去吃肉
    2020-11-27 10:06

    There is no reduce version that takes two different types without a combiner since it can't be executed in parallel (not sure why this is a requirement). The fact that accumulator must be associative makes this interface pretty much useless since:

    list.stream().reduce(identity,
                         accumulator,
                         combiner);
    

    Produces the same results as:

    list.stream().map(i -> accumulator(identity, i))
                 .reduce(identity,
                         combiner);
    

提交回复
热议问题