What is the LongFunction interface in Java?
Overview
LongFunction is a functional interface that accepts one argument of type long and returns back a result. The interface contains one method, apply.
The LongFunction interface is defined in the java.util.function package. To import the LongFunction interface, check the following import statement.
import java.util.function.LongFunction;
apply(long value) method
This method applies the functionality to the given argument of type long. This is the functional method of the interface.
Syntax
R apply(long value);
Parameters
long value: This is the function argument of typelong.
Return type
The method returns the function result.
Code
import java.util.function.LongFunction;public class Main{public static void main(String[] args) {LongFunction<String> stringLongFunction = (d) -> String.format("The passed value is %s", d);long l = 100;// calling apply method of the LongFunctionSystem.out.println(stringLongFunction.apply(l));}}
Explanation
- Line 1: We import the
LongFunctioninterface. - Line 5: We define an implementation of the
LongFunctioninterface calledstringLongFunctionthat returns a string. - Line 6: We define a long value called
l. - Line 9: We print the result of using the
apply()method passinglas an argument.