Java8 - java.util.function.Function Interface tutorial with Examples
java util Function class
This is a simple Functional interface, with which the method takes as input and returns the output. This interface is used to map the objects - which takes one type as input, processes it, and returns the output in another type. This interface has only a single abstract method.java.util.function.The function is a predefined interface introduced in java 8.
This post is about the predefined functional interface - Function examples with tutorials. And also explained about usage of apply(), andThen(), componse() method with examples
public interface Function {
public R apply(T t);
}
T - input value as given R - output or return value. Here object type T is input to a lambda expression, process, and Object T is returned as the value.
java util Function class methods
Default methods are andThen() and compose() methods, Static methods are identity() method
Function interface apply method example
This function applies the method takes the input is string and output is a string. apply() method process string and return the string as the value
import java.util.function.Function;
public class MyFunctionExample {
static String HelloWorld(String name) {
return "Hello " + name;
}
public static void main(String[] args) {
Function<String, String> functionalVariable = MyFunctionInterface::HelloWorld;
System.out.println(functionalVariable.apply("Frank"));
}
}
and output is
Hello Frank
and one more example of apply() method example
import java.util.function.Function;
public class MyFunctionExample {
public static void main(String[] args) {
Function<Integer, Integer> IncrementFunction = (value) -> (value + 1);
System.out.println(IncrementFunction.apply(2));
System.out.println(IncrementFunction.apply(5));
}
}
and output of the above code is
output is 3,6
Function interface andThen method example
andThen() is default method. It combines the called function with other functions after the function is applied.
Syntax
default <V> Function<T,V> andThen(Function<? super R,? extends V> after)
parameter - after the function
import java.util.function.Function;
public class MyFunctionExample {
public static void main(String[] args) {
Function<Integer, Integer> IncrementFunction = (value) -> (value + 1);
Function<Integer, Integer> doubleFunction = value -> 2*value;
double output=IncrementFunction.andThen(doubleFunction).apply(10);
System.out.println("output of andThen: "output);
}
}
The above code Output is
the output of andThen: 22.0
Function interface compose method example
It is the default method. It combines the called function with other functions before the function is applied. Syntax
default <V> Function<V,R> compose(Function<? super V,? extends T> before)
import java.util.function.Function;
public class MyFunctionExample {
public static void main(String[] args) {
Function<Integer, Integer> IncrementFunction = (value) -> (value + 1);
Function<Integer, Integer> doubleFunction = value -> 2*value;
double output=IncrementFunction.compose(doubleFunction).apply(10);
System.out.println("the output of composing: "+output);
}
}
The output of the above code is
the output of composing: 21.0