java.util.function.Function
is part of java.util.function
package.
In Java 8, Function
is a functional interface. It accepts one argument(object of type T) and produces a result (object of type R).
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
Here, T is the Type of input to the function.
R is the type of result of the function.
In addition to abstract method apply()
, this interface has two default methods andThen()
and compose()
. Function interface has one static method – identity()
.
default <V> Function<V,R> andThen(Function<? super R,? extends V> after) | This method returns a composed function that first applies this function to its input, and then applies the after result to the result. |
default <V> Function<V,R> compose(Function<? super V, ? extends T> before) | This method returns a composed function that first applies the before function to its input, and then applies this function to the result. |
static <T> Function<T,T> identity() | This method returns a function that always returns its input argument. |
Example of Function interface
We’ll use Function
interface to find the length of string. This method takes String as input and returns Integer as output.
public class FunctionExample { public static void main(String[] args) { Function<String, Integer> func = s -> s.length(); Integer length = func.apply("hello world"); System.out.println("Length of string is: " + length); } }
Output
Length of string is: 11
Chaining of Function with example of andThen()
Function chaining is nothing but syntax to invoke multiple methods calls in single line. In this example, we’ll chain Function with andThen()
.
In this example, we’ll change string to uppercase and then will find substring with first 5 characters.
import java.util.function.Function; public class FunctionChainWithAndThen { public static void main(String[] args) { Function<String, String> func1 = s -> s.toUpperCase(); Function<String, String> func2 = s -> s.substring(0,5); String result = func1.andThen(func2).apply("hello world"); System.out.println("result is: " + result); } }
Output
result is: HELLO
Example of compose() method
In the following example, first func2 will be applied. For the input integer 2, first i+2 (result is 4) will be calculated. Then square of 4 (result is 16) will be calculated.
import java.util.function.Function; public class FunctionChainWithAndThen { public static void main(String[] args) { Function<Integer, Integer> func1 = i -> i*i; Function<Integer, Integer> func2 = i -> i+2; Integer result = func1.compose(func2).apply(2); System.out.println("result is: " + result); } }
Output
result is: 16
Example of identity() method
import java.util.function.Function; public class IdentityExample { public static void main(String[] args) { Function<String, String> func = Function.identity(); String result = func.apply("hello world"); System.out.println(result); } }
Output
hello world
Explanation of this code is simple. For the given string “hello world”, method returns the same input argument as the result.