The Function Interface in Java is a built-in functional interface from the java.util.function package introduced in Java 8. It represents a function that accepts a single input and returns a transformed result. It is widely used with lambda expressions and method references to write concise, reusable, and functional-style code.
- It accepts one input argument and produces a single output value.
- It is commonly used for transforming, converting, or mapping data from one type to another.
- It plays an important role in the Stream API and other functional programming features introduced in Java 8.
Syntax
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
- T : Represents the type of the input argument.
- R : Represents the type of the result returned by the function.
Methods in Function Interface
The Function interface consists of the following 4 methods, as listed, which are later discussed as follows:
1. apply()
Applies the function to the given input and returns the transformed result.
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.
import java.util.function.Function;
// Main class
public class Geeks {
// Main driver method
public static void main(String args[])
{
// Function which takes in a number
// and returns half of it
Function<Integer, Double> half = a -> a / 2.0;
// Applying the function to get the result
System.out.println(half.apply(10));
}
}
Output
5.0
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
This method throws NullPointerException if the after function is null.
import java.util.function.Function;
// Main class
public class Geeks {
// main driver method
public static void main(String args[])
{
// Function which takes in a number and
// returns half of it
Function<Integer, Double> half = a -> a / 2.0;
// Now triple the output of half function
half = half.andThen(a -> 3 * a);
// Applying the function to get the result
// and printing on console
System.out.println(half.apply(10));
}
}
Output
15.0
Example To demonstrate when NullPointerException is returned.
This example demonstrates that the andThen() method throws a NullPointerException when a null function is passed instead of a valid function.
import java.util.function.Function;
// Main class
public class Geeks {
// Main driver method
public static void main(String args[])
{
// Function which takes in a number and
// returns half of it
Function<Integer, Double> half = a -> a / 2.0;
// Try block to check for exceptions
try {
// Trying to pass null as parameter
half = half.andThen(null);
}
// Catch block to handle exceptions
catch (Exception e) {
// Print statement
System.out.println("Exception thrown "
+ "while passing null: "
+ e);
}
}
}
Output
Exception thrown while passing null: java.lang.NullPointerException
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
This method throws NullPointerException if the before function is null.
import java.util.function.Function;
// Main class
public class Geeks {
// Main driver method
public static void main(String args[])
{
// Function which takes in a number and
// returns half of it
Function<Integer, Double> half = a -> a / 2.0;
// Triple the value given to half function
half = half.compose(a -> 3 * a);
// Applying the function to get the result
System.out.println(half.apply(5));
}
}
Output
7.5
Example: Program to Demonstrate when NullPointerException is returned.
import java.util.function.Function;
// Main class
public class Geeks {
// Main driver method
public static void main(String args[])
{
// Function which takes in a number and
// returns half of it
Function<Integer, Double> half = a -> a / 2.0;
// Try block to check for exceptions
try {
// Trying to pass null as parameter
half = half.compose(null);
}
// Catch block to handle exceptions
catch (Exception e) {
// Print statement
System.out.println("Exception thrown "
+ "while passing null: "
+ e);
}
}
}
Output
Exception thrown while passing null: java.lang.NullPointerException
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.
import java.util.function.Function;
// Main class
public class Geeks {
// Main driver method
public static void main(String args[])
{
// Function which takes in a number and
// returns it
Function<Integer, Integer> i = Function.identity();
System.out.println(i.apply(10));
}
}
Output
10
Applying Function on Objects and Handling Multiple Conditions
1. Using Function on Objects
import java.util.function.Function;
// create a Person class with properties
// name and age
class Person {
String name;
int age;
// creates a constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
// override the toString() method to
// display the person details
@Override
public String toString() {
return "Name: " + name + ", Age: " + age;
}
}
public class Geeks {
public static void main(String args[]) {
// create a Function that takes object and
// returns a greeting with the person's name
Function<Person, String> greet = person -> "Hello, " + person.name;
Person p = new Person("Geek1", 25);
System.out.println(greet.apply(p));
}
}
Output
Hello, Geek1
Explanation: In the above example we are using a Function interface to transform the Person object into a greeting string which contains the person's name.
2. Handling Multiple Conditions Using Function
import java.util.function.Function;
public class Geeks {
public static void main(String args[]) {
Function<Integer, Integer> addFive = a -> a + 5;
Function<Integer, Integer> multiplyByTwo = a -> a * 2;
// Applying functions sequentially: Add five, then multiply by two
Function<Integer, Integer> result = addFive.andThen(multiplyByTwo);
System.out.println(result.apply(3));
}
}
Output
16
Explanation: In the above example, two functions are declared with the help of Function interface. The first function takes an integer and add 5 to it and the second function takes an integer and then multiply it with 2. These two functions are combined with andThen() method. This method make sure that the addFive() function is applied first and then multipleByTwo() function is applied.