The code below contains the two- and three-parameter
reduce() stream operation/method, on lines 1 through 3 and lines 5 through 7, respectively. While the three-parameter method successfully reduces the stream items to the
string “
wolf,” the compiler reports an error when an attempt is made to use the two-parameter method to reduce the stream items shown in the code. The error message reported by the compiler is also provided below.
The two-parameter
reduce() stream operation/method requires the following, when the method is invoked: The type of the first, identity argument passed to the method must be the same as the (single) type of the
BinaryOperator lambda passed as the second, accumulator argument to the method. In the particular case of the code for the two-parameter method above (on lines 1 through 3), these types that must be the same must be the
Character type (since the stream items are
char types). If I have understood the compiler error correctly, in summary, it simply indicates that an identity argument of the wrong type (i.e., the
String, rather than the
Character, type) has been provided.
The three-parameter
reduce() stream operation/method requires the following, when the method is invoked: The type of the first, identity argument passed to the method must be the same as the (single) type of the
BinaryOperator lambda passed as the third, combiner argument to the method as well as the type of the first parameter and return type of the
BiFunction lambda passed as the second, accumulator argument to the method. In the particular case of the code for the three-parameter method above (on lines 5 through 7), the type of these types that must be the same is the
String type.
Since the stream items in the code shown are
char types, I expect that the type, of the first parameter of the
BiFunction lambda passed as the second, accumulator argument to the three-parameter method, should be
Character and not
String. However, it appears like the compiler is okay with the three-parameter method having the type of this first parameter of the
BiFunction lambda being
String, rather than
Character. I am unable to see why the compiler is okay with this, even though I do understand why it would be okay with the (single) type of the
BinaryOperator lambda passed as the third, combiner argument to the method to be
String.
Since the stream items in the code shown are
char types, I do understand the reason for the compiler action described in the point number 1 below, but I do not understand the reason for the compiler action described in the point number 2 below:
(1.) The compiler is okay with the two parameters, of the
BinaryOperator lambda passed as the second, accumulator argument to the two-parameter method, being the
Character type.
(2.) The compiler is okay with the first parameter, of the
BiFunction lambda passed as the second, accumulator argument to the three-parameter method, being the
String type (shouldn't this type be
Character and not
String?).
Could the reason, for the compiler action mentioned in the point number 2 above, be because the accumulator operates on intermediate reductions that would be strings, since these intermediate reductions would be strings that are produced/generated when the combiner concatenates the empty string identity with pairs of the
char types in the stream shown in the code (I have noticed that applying the "+" operator to a pair of
char types evaluates to an
int type, but applying the operator to
char types and a string, including an empty string, evaluates to a
String type)?