Function Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expect one argument as an input and produces a result. As it is a functional interface, it can be used assignment target for lambda expression or method reference. In this post, we are going to see several implementations of Function Interface by using different examples.
Function Interface in Java 8 with Examples...!!! Share on X
Look at Function Javadoc description below:
Function Interface contains 4 methods:
apply
compose
andThen
identity
Let’s discuss these methods:
apply
This method performs operation on the given argument and return the function result.
1 | R apply(T t); |
compose
This method returns a composed function that first applies the before function to its input, and then applies this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.
1 2 3 4 | default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } |
andThen
This method returns a composed function that first applies this function to its input, and then applies the after function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.
1 2 3 4 | default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } |
identity
This method returns a function that always returns its input argument.
1 2 3 | static <T> Function<T, T> identity() { return t -> t; } |
Lets understand above mentioned methods thorough various examples:
Example 1. ‘apply’ method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.function.Function; public class FunctionInterfaceJava8Example1 { public static void main(String[] args) { System.out.println("Function Interface - 'apply' example \n"); Function <Integer, Integer> functionObj = i -> i * 2; System.out.println("Double of 5 : " + functionObj.apply(5)); } } |
Example 2. ‘andThen’ & ‘compose’ methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.function.Function; public class FunctionInterfaceJava8Example2 { public static void main(String[] args) { System.out.println("Function Interface - 'andThen' & 'compose' example \n"); Function <Integer, Integer> functionObj1 = i -> i * 10; Function <Integer, Integer> functionObj2 = i -> i / 2; //andThen operation System.out.println("Result of 'andThen' method: " + functionObj1.andThen(functionObj2).apply(4)); //compose operation System.out.println("Result of 'compose' method: " + functionObj1.compose(functionObj2).apply(4)); } } |
Function Interface in Java 8 with Examples...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Supplier Interface in Java 8 with Examples