The ToDoubleBiFunction is a functional interface from the java.util.function package introduced in Java 8. It represents a function that accepts two input arguments and returns a primitive double value. It is useful when an operation requires two inputs and the result specifically needs to be a double.
Example: Following example uses ToDoubleBiFunction to multiply the second input by 3.2 and add the result to the first input.
import java.util.function.ToDoubleBiFunction;
public class GFG{
public static void main(String[] args) {
ToDoubleBiFunction<Integer, String> operation =
(a, b) -> a + Integer.parseInt(b) * 3.2;
double result = operation.applyAsDouble(3, "10");
System.out.println(result);
}
}
Output
35.0
Explanation: The function accepts an Integer and a String. The lambda expression converts "10" to an integer, multiplies it by 3.2, and adds 3, producing 35.0.
Syntax
@FunctionalInterface
public interface ToDoubleBiFunction<T, U> {
double applyAsDouble(T t, U u);
}
Here:
- T represents the type of the first input argument.
- U represents the type of the second input argument.
- applyAsDouble() applies the defined operation to both arguments and returns a primitive double.
How ToDoubleBiFunction Works
ToDoubleBiFunction takes two input values, applies the defined operation, and returns the result as a primitive double.
- Define Input Types: T represents the first input type, while U represents the second input type.
- Create the Function: A lambda expression or method reference defines the operation performed on both inputs.
- Call applyAsDouble(): The method receives the two input values and executes the defined operation.
- Return the Result: The operation produces and returns a primitive double value.
For example:
ToDoubleBiFunction<Double, Integer> totalPrice =
(price, quantity) -> price * quantity;double result = totalPrice.applyAsDouble(99.5, 4);
System.out.println(result);
Output:
398.0
Here, the function accepts a Double representing the price and an Integer representing the quantity. It multiplies the two values and returns 398.0 as a primitive double.
Method Used By ToDoubleBiFunction
applyAsDouble()
The applyAsDouble() method applies the defined operation to two input arguments and returns the resulting double value.
Syntax
double applyAsDouble(T t, U u)
Parameters:
- t – the first input argument of type T.
- u – the second input argument of type U.
Returns: A double result produced by the function.
import java.util.function.ToDoubleBiFunction;
public class GFG{
public static void main(String[] args) {
ToDoubleBiFunction<Integer, Double> calculateCost =
(quantity, price) -> quantity * price;
double result = calculateCost.applyAsDouble(5, 49.5);
System.out.println(result);
}
}
Output
247.5
Explanation: The function takes 5 as the quantity and 49.5 as the price. The lambda expression multiplies these values, and applyAsDouble() returns 247.5.
ToDoubleBiFunction Vs BiFunction
ToDoubleBiFunction<T, U> is similar to BiFunction<T, U, Double>, but ToDoubleBiFunction directly returns the primitive double type.
| Feature | ToDoubleBiFunction<T, U> | BiFunction<T, U, Double> |
|---|---|---|
| First input | T | T |
| Second input | U | U |
| Return type | double | Double |
| Method | applyAsDouble() | apply() |
| Number of inputs | 2 | 2 |