So im learning on how to use Lambda in Java and got the problem, that Sonar Lint is saying i should refactor the code to use the more specialised functional interface.
public float durchschnitt(float zahl1, float zahl2) {
BiFunction<Float, Float, Float> function = (Float ersteZahl, Float zweiteZahl) -> (ersteZahl + zweiteZahl) / 2;
// ^
// Here I get the warning:
// SonarLint: Refactor this code to use the more specialised Functional Interface 'BinaryOperator<Float>'
return function.apply(zahl1, zahl2);
}
All this little program should do is calculate the average of the two floats. The programm's working fine, but I want the warning to be gone. So how can I avoid that waring and fix the code?
Edit: I oc tried finding a solution on google etc., but found none.
The
BinaryOperator<T>
is in fact a subinterface ofBiFunction<T, T, T>
, and its documentation states "this is a specialization of BiFunction for the case where the operands and the result are all of the same type", so just replace with:Also no need to declare the
Float
argument types, it is auto-inferred by the compiler: