The ToIntBiFunction is a functional interface from the java.util.function package introduced in Java 8. It represents a function that accepts two arguments of different types and produces an int result.
- T represents the type of the first input argument.
- U represents the type of the second input argument.
- The applyAsInt() method applies an operation to both inputs and returns an int value.
Example: Program to use of ToIntBiFunction to multiply the second input by 3 and add the result to the first input.
import java.util.function.ToIntBiFunction;
public class GFG{
public static void main(String[] args) {
ToIntBiFunction<Integer, String> function =
(num, str) -> num + Integer.parseInt(str) * 3;
int result = function.applyAsInt(3, "10");
System.out.println(result);
}
}
Output
33
Explanation: Here, the first input is an Integer and the second input is a String. The lambda expression converts "10" into an integer, multiplies it by 3, and adds 3. Therefore, the function returns 33 as a primitive int.
Syntax
@FunctionalInterface
public interface ToIntBiFunction<T, U> {
int applyAsInt(T t, U u);
}
Here, T and U specify the types of the two input arguments. Unlike a regular BiFunction, the return type does not need to be specified because ToIntBiFunction always returns a primitive int.
Working Of ToIntBiFunction
ToIntBiFunction takes two input values, applies the defined operation, and returns the result as a primitive int.
- 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 applyAsInt(): The method receives the two input values and executes the defined operation.
- Return the Result: The operation produces and returns a primitive int value.
For example:
ToIntBiFunction<String, String> totalLength =
(str1, str2) -> str1.length() + str2.length();int result = totalLength.applyAsInt("Java", "Programming");
System.out.println(result);
Output:
15
Here, the function accepts two String values and returns the sum of their lengths as an int.
applyAsInt()
The applyAsInt() method applies the defined operation to two input arguments and returns the resulting int value.
Syntax
int applyAsInt(T t, U u)
Parameters:
- t – the first input argument of type T.
- u – the second input argument of type U.
Returns: An int result produced by the function.
import java.util.function.ToIntBiFunction;
public class GFG{
public static void main(String[] args) {
ToIntBiFunction<String, Integer> calculate =
(str, num) -> str.length() * num;
int result = calculate.applyAsInt("Java", 5);
System.out.println(result);
}
}
Output
20
Explanation: The first input "Java" has a length of 4, while the second input is 5. The lambda expression multiplies these values and returns 20.
ToIntBiFunction Vs BiFunction
ToIntBiFunction<T, U> is similar to BiFunction<T, U, Integer>, but ToIntBiFunction directly returns the primitive int type.
| Feature | ToIntBiFunction<T, U> | BiFunction<T, U, Integer> |
|---|---|---|
| First input | T | T |
| Second input | U | U |
| Return type | int | Integer |
| Method | applyAsInt() | apply() |
| Number of inputs | 2 | 2 |