ToLongBiFunction Interface in Java with Examples - GeeksforGeeks

ToLongBiFunction Interface in Java with Examples

Last Updated : 14 Sep, 2026

ToLongBiFunction 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 a primitive long result.

Example: Program uses ToLongBiFunction to combine an integer and a numeric string and produce a long result.

Java
import java.util.function.ToLongBiFunction;

public class GFG {
    public static void main(String[] args) {

        ToLongBiFunction<Integer, String> operation =
            (a, b) -> a + Long.parseLong(b) * 1000000L;

        System.out.println(operation.applyAsLong(3, "10"));
    }
}

Output
10000003

Explanation: Here, T is Integer and U is String. The lambda expression converts the string "10" into a long, multiplies it by 1,000,000, adds 3, and returns the result as a long.

Syntax

The syntax of the ToLongBiFunction interface is:

@FunctionalInterface
public interface ToLongBiFunction<T, U> {
long applyAsLong(T t, U u);
}

Here:

  • T represents the type of the first input argument.
  • U represents the type of the second input argument.
  • applyAsLong() applies the defined operation to both arguments and returns a primitive long.

Unlike a regular BiFunction, the return type does not need to be specified as a generic type because ToLongBiFunction always returns a long.

Working Of ToLongBiFunction

ToLongBiFunction takes two input values, applies the defined operation, and returns the result as a primitive long.

  • Define Input Types: T represents the type of the first input, while U represents the type of the second input.
  • Create the Function: A lambda expression or method reference defines the operation performed on the two inputs.
  • Call applyAsLong(): The method receives the two input values and executes the defined operation.
  • Return the Result: The operation produces and returns a primitive long value.

For example:

ToLongBiFunction<Integer, Long> calculate =
(quantity, value) -> quantity * value;
long result = calculate.applyAsLong(5, 100000L);
System.out.println(result);

Output:

500000

ere, the function accepts an Integer and a Long. It multiplies the two values and returns 500000 as a primitive long.

Method Of ToLongBiFunction

applyAsLong()

The applyAsLong() method applies the defined operation to two input arguments and returns a long result.

Syntax

long applyAsLong(T t, U u)

Parameters:

  • t – the first input argument of type T.
  • u – the second input argument of type U.

Returns: A long result produced by the function.

Java
import java.util.function.ToLongBiFunction;

public class GFG{
    public static void main(String[] args) {

        ToLongBiFunction<Integer, Long> totalUnits =
                (groups, units) -> groups * units;

        long result = totalUnits.applyAsLong(4, 250000L);

        System.out.println(result);
    }
}

Output
1000000

Explanation: The function takes 4 as the number of groups and 250000 as the number of units in each group. The lambda expression multiplies the two values, and applyAsLong() returns 1000000.

ToLongBiFunction Vs BiFunction

ToLongBiFunction<T, U> and BiFunction<T, U, Long> both accept two arguments and produce a Long result. The main difference is that ToLongBiFunction directly returns the primitive long type.

FeatureToLongBiFunction<T, U>BiFunction<T, U, Long>
First inputTT
Second inputUU
Return typelongLong
MethodapplyAsLong()apply()
Number of inputs22
Comment