The LongFunction interface is a functional interface from the java.util.function package, introduced in Java 8. It represents a function that accepts a long value and produces a result of type R.
- Uses the generic type R to define the return type.
- Useful when the input is a primitive long and the operation produces a result of another type.
- Similar to Function<Long, R>, but LongFunction directly accepts a long value.
Example: The following program, uses a lambda expression with LongFunction to divide a long value by 2.
import java.util.function.LongFunction;
public class GFG {
public static void main(String[] args) {
LongFunction<Double> operation = value -> value / 2.0;
System.out.println(operation.apply(3L));
}
}
Output
1.5
Explanation: The lambda expression divides the input value by 2.0. The apply() method executes the operation for 3L and returns 1.5.
Syntax
@FunctionalInterface
public interface LongFunction<R> {
R apply(long value);
}
Note: LongFunction<R> is a specialization of Function<Long, R> that accepts a primitive long, avoiding boxing of the input value into Long.
Working of LongFunction
The LongFunction interface separates the input type from the result type. It always accepts a primitive long value, while the generic type R determines the type of result produced by the operation.
The working process is as follows:
- A lambda expression is assigned to a LongFunction object.
- The lambda expression provides the implementation of the apply() method.
- When apply() is called, the supplied primitive long value is passed to the lambda expression.
- The operation defined in the lambda expression is performed.
- The resulting value is returned as type R.
Example: Program to demonstrates how LongFunction converts a primitive long value into a String.
import java.util.function.LongFunction;
public class GFG {
public static void main(String[] args) {
LongFunction<String> operation =
value -> "Number is " + value;
String result = operation.apply(100L);
System.out.println(result);
}
}
Output
Number is 100
Explanation: The value 100L is supplied to apply(). The lambda converts the value into a String and returns "Number is 100".
apply() Method
The apply() method is the single abstract method of the LongFunction interface. It accepts a primitive long value, performs the operation defined by the function, and returns a result of type R.
Syntax
R apply(long value)
- Parameter: value: The primitive long value passed to the function.
- Return Value: The method returns the result produced by the function, of type R.
Example: Program to demonstrates how the apply() method executes a lambda expression that calculates the square of a given long value.
import java.util.function.LongFunction;
public class GFG{
public static void main(String[] args) {
LongFunction<String> square =
value -> "Square is " + (value * value);
System.out.println(square.apply(5L));
}
}
Output
Square is 25
Explanation: The apply(5L) method passes 5L to the lambda expression. The expression calculates the square of the value and returns the result as a string.