Function Interface
This lesson explains the Function interface in Java.
We'll cover the following
Function
is a category of functional interfaces that takes an object of type T
and returns an object of type R
.
Until now, the functional interfaces that we’ve discussed have either not taken any argument(Supplier
), not returned any value(Consumer
), or returned only a boolean(Predicate
).
Function
interfaces are very useful as we can specify the type of input and output.
Below are some of the interfaces that fall in this category.
Let us discuss some types of Function
functional interfaces.
Function<T, R>
The function takes only one argument of type T
and returns a result of type R
.
The following is the list of all the methods in the Function<T, R>
interface.
Let’s look at an example of each method:
R apply(T t)
This is the abstract method of the Function
interface. It takes one argument of type T
as input and returns a value of type R
.
In the below example, we will create a function called lengthFunction
. It takes a string as input and returns the length of the string as output.
import java.util.function.Function;public class FunctionInterfaceDemo {public static void main(String[] args) {// Created a function which returns the length of string.Function<String, Integer> lengthFunction = str -> str.length();System.out.println("String length: " + lengthFunction.apply("This is awesome!!"));}}
compose(Function<? super V, ? extends T> before)
Returns a composed function that first applies the function provided as a parameter on the input, and then applies the function on which it is called, to the result.
import java.util.function.Function;public class FunctionDemo {public static void main(String args[]) {// Function which adds 10 to the given element.Function<Integer, Integer> increment = x -> x + 10;// Function which doubles the given element.Function<Integer, Integer> multiply = y -> y * 2;// Since we are using compose(), multiplication will be done first and then increment will be done.System.out.println("compose result: " + increment.compose(multiply).apply(3));}}
andThen(Function<? super R,? extends V> after)
This method returns a composed function that first applies the function on which it is called on the input, and then applies the function provided as parameter, to the result.
import java.util.function.Function;public class FunctionDemo {public static void main(String args[]) {Function<Integer,Integer> increment = x -> x + 10;Function<Integer,Integer> multiply = y -> y * 2;// Since we are using andThen(), increment will be done first and then multiplication will be done.System.out.println("andThen result: " + increment.andThen(multiply).apply(3));}}
BiFunction<T,U,R>
The BiFunction<T, U, R>
is similar to Function<T, R>
interface; the only difference is that the BiFunction
interface takes in two parameters and returns an output.
Below is the list of methods in the BiFunction
interface.
In the below example, we will create a BiFunction
that takes two numbers as input and returns their sum.
import java.util.function.BiFunction;public class BiFunctionInterfaceDemo {public static void main(String args[]){BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;System.out.println("Sum = " + add.apply(2, 3));}}
In the next lesson, you will learn about a subtype of the Function
interface, the Unary
operator.
Get hands-on with 1300+ tech skills courses.