Function
is a functional interface that accepts one argument and returns a result. The apply()
method of the interface is the functional method.
The Function
interface is defined in the java.util.function
package. To import the Function
interface, check the following import statement:
import java.util.function.Function;
R apply(T t);
T t
: This is the function argument.The method returns the function result.
import java.util.function.Function;public class Main {public static void main(String[] args){Function<Integer, Integer> addFunction = arg -> arg + 5;int arg = 1;System.out.println("Result of apply() on addFunction --> " + addFunction.apply(arg));}}
Function
interface from the function
package.Function
interface called addFunction
. addFunction
accepts an integer and returns the result of adding 5
to the given integer.arg
.apply()
method and pass arg
as an argument.