Open In App

Function Interface in Java

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
22 Likes
Like
Report

The Function Interface is a part of the java.util.function package that has been introduced since Java 8, to implement functional programming in Java. It represents a function that 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

Note: 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:

  1. apply()
  2. andThen()
  3. compose()
  4. 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:


Output
5.0


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:


Output
15.0


Example 2: To demonstrate when NullPointerException is returned.


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:


Output
7.5


Example 2: When NullPointerException is returned.


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: 


Output
10


Applying Function on Objects and Handling Multiple Conditions

1. Using Function on Objects

Example:


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

Example:


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.


Similar Reads