Why does the battery in Stream :: reduce the BiFunction and not the BinaryOperator as a combiner?

Why is the battery argument in method Stream::reducea BiFunction, not a BinaryOperator, as the argument of the combiner.

Why his type BiFunction<U, ? super T, U>? Why T? Should it BiFunction<U, ? extends U, U>?

+4
source share
1 answer
<U> U reduce(U identity,
             BiFunction<U, ? super T, U> accumulator,
             BinaryOperator<U> combiner);

A battery is a function that adds an element Stream(whose type is indicated T) to the intermediate result of the operation reduce(whose type is indicated U) and returns an updated result (also of type U).

BinaryOperator, .

, reduce a BiFunction<Integer,String,Integer>, Stream<String> . BinaryOperator<Integer> BinaryOperator<String>.

, ( U) , U. a BinaryOperator<U> ( BiFunction<U,U,U>).

+6

Source: https://habr.com/ru/post/1663213/


All Articles