0

I wonder why ToDoubleBiFunction doesn't extend BiFunction. Just how I did:

import java.util.function.BiFunction;

public class Test {
  public static void main(String[] args) {
    MyToDoubleBiFunction<Integer, Integer> f = (a, b) -> a.doubleValue() + b.doubleValue();
    System.out.println(f.apply(1, 2));
  }
}

@FunctionalInterface
interface MyToDoubleBiFunction<T, U> extends BiFunction<T, U, Double> {
}

This code works and prints 3.0.

Also what is the point of using wordy applyAsDouble method instead of well-known apply?

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • The result type of `MyToDoubleBiFunction.apply` is `Double` whereas it is `double` for `ToDoubleBiFunction.applyAsDouble`. – LuCio Sep 27 '18 at 20:24

1 Answers1

4

The point of specific versions of BiFunction such as ToDoubleBiFunction is to avoid auto(un)boxing - in this case, autoboxing / -unboxing a primitive double value to / from a java.lang.Double object.

If ToDoubleBiFunction would extend BiFunction<T, U, Double> then you'd have auto(un)boxing.

Note that you cannot use primitive types as type arguments in Java, so BiFunction<T, U, double> (note that double is the primitive type here) is not possible.

Jesper
  • 202,709
  • 46
  • 318
  • 350