Why is the accumulator argument in the Stream::reduce method a BiFunction and not a BinaryOperator like the combiner argument.
Why is its type BiFunction<U, ? super T, U>? Why T? Should it be BiFunction<U, ? extends U, U>?
Why is the accumulator argument in the Stream::reduce method a BiFunction and not a BinaryOperator like the combiner argument.
Why is its type BiFunction<U, ? super T, U>? Why T? Should it be BiFunction<U, ? extends U, U>?
<U> U reduce(U identity,
BiFunction<U, ? super T, U> accumulator,
BinaryOperator<U> combiner);
The accumulator is a function that adds an element of the Stream (whose type is denoted by T) to the intermediate result of the reduce operation (whose type is denoted by U) and returns an updated result (also of type U).
Therefore you can't define it as a BinaryOperator, where the operands and result are all of the same type.
For example, you might pass as the accumulator in a reduce call a BiFunction<Integer,String,Integer> which is applied on a Stream<String> and produces the sum of the lengths of all the elements. You can't use a BinaryOperator<Integer> or a BinaryOperator<String> for that.
On the other hand, the combiner takes two intermediate results (both of the same type U) and merges them into a result whose type is also U. Therefore a BinaryOperator<U> (which extends BiFunction<U,U,U>) can be used.