The Function Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and produces a result. Hence this functional interface takes in 2 generics namely as follows:
- T: denotes the type of the input argument
- R: denotes the return type of the function
The lambda expression assigned to an object of Function type is used to define its apply() which eventually applies the given function on the argument.
Methods in Function Interface
The Function interface consists of the following 4 methods as listed which are later discussed as follows:
- apply()
- andThen()
- compose()
- identity()
Method 1: apply()
Syntax:
R apply(T t)
Parameters: This method takes in only one parameter t which is the function argument
Return Type: This method returns the function result which is of type R.
Example
Java
import java.util.function.Function;
public class GFG {
public static void main(String args[])
{
Function<Integer, Double> half = a -> a / 2.0 ;
System.out.println(half.apply( 10 ));
}
}
|
Method 2: andThen()
It returns a composed function wherein the parameterized function will be executed after the first one. If evaluation of either function throws an error, it is relayed to the caller of the composed function.
Syntax:
default <V> Function<T, V>
andThen(Function<? super R, ? extends V> after)
where V is the type of output of the after function, and of the composed function
Parameters: This method accepts a parameter after which is the function to be applied after the current one.
Return Value: This method returns a composed function that applies the current function first and then the after function
Exception: This method throws NullPointerException if the after function is null.
Example 1:
Java
import java.util.function.Function;
public class GFG {
public static void main(String args[])
{
Function<Integer, Double> half = a -> a / 2.0 ;
half = half.andThen(a -> 3 * a);
System.out.println(half.apply( 10 ));
}
}
|
Example 2: To demonstrate when NullPointerException is returned.
Java
import java.util.function.Function;
public class GFG {
public static void main(String args[])
{
Function<Integer, Double> half = a -> a / 2.0 ;
try {
half = half.andThen( null );
}
catch (Exception e) {
System.out.println( "Exception thrown "
+ "while passing null: "
+ e);
}
}
}
|
Output
Exception thrown while passing null: java.lang.NullPointerException
Method 3: compose()
It returns a composed function wherein the parameterized function will be executed first and then the first one. If evaluation of either function throws an error, it is relayed to the caller of the composed function.
Syntax:
default <V> Function<V, R>
compose(Function<? super V, ? extends T> before)
Where V is the type of input of the before function, and of the composed function
Parameters: This method accepts a parameter before which is the function to be applied first and then the current one
Return Value: This method returns a composed function that applies the current function after the parameterized function
Exception: This method throws NullPointerException if the before function is null.
Example 1:
Java
import java.util.function.Function;
public class GFG {
public static void main(String args[])
{
Function<Integer, Double> half = a -> a / 2.0 ;
half = half.compose(a -> 3 * a);
System.out.println(half.apply( 5 ));
}
}
|
Example 2: When NullPointerException is returned.
Java
import java.util.function.Function;
public class GFG {
public static void main(String args[])
{
Function<Integer, Double> half = a -> a / 2.0 ;
try {
half = half.compose( null );
}
catch (Exception e) {
System.out.println( "Exception thrown "
+ "while passing null: "
+ e);
}
}
}
|
Output
Exception thrown while passing null: java.lang.NullPointerException
Method 4: identity()
This method returns a function that returns its only argument.
Syntax:
static <T> Function<T, T> identity()
where T denotes the type of the argument and the value to be returned
Returns: This method returns a function that returns its own argument
Example
Java
import java.util.function.Function;
public class GFG {
public static void main(String args[])
{
Function<Integer, Integer> i = Function.identity();
System.out.println(i.apply( 10 ));
}
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!