Function Interface in Java with Examples

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 which takes in 2 generics namely:-

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

1. apply()

This method applies the given function on its only argument.

Syntax:

R apply(T t)

Parameters: This method takes in only one parameter t which is the function argument

Returns: This method returns the function result which is of type R.

Below is the code to illustrate apply() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.function.Function;
  
public class Main {
    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;
  
        // apply the function to get the result
        System.out.println(half.apply(10));
    }
}

chevron_right


Output:

5.0

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html#apply-T-

2. addThen()

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.

Below is the code to illustrate addThen() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.function.Function;
  
public class Main {
    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 treble the output of half function
        half = half.andThen(a -> 3 * a);
  
        // apply the function to get the result
        System.out.println(half.apply(10));
    }
}

chevron_right


Output:

15.0

Program 2: To demonstrate when NullPointerException is returned.

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.function.Function;
  
public class Main {
    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 {
  
            // try to pass null as parameter
            half = half.andThen(null);
        }
        catch (Exception e) {
            System.out.println("Exception thrown "
                               + "while passing null: " + e);
        }
    }
}

chevron_right


Output:


Exception thrown while passing null: java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html#andThen-java.util.function.Function-

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.

Below is the code to illustrate compose() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.function.Function;
  
public class Main {
    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;
  
        // However treble the value given to half function
        half = half.compose(a -> 3 * a);
  
        // apply the function to get the result
        System.out.println(half.apply(5));
    }
}

chevron_right


Output:


7.5

Program 2: To demonstrate when NullPointerException is returned.

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.function.Function;
  
public class Main {
    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 {
  
            // try to pass null as parameter
            half = half.compose(null);
        }
        catch (Exception e) {
            System.out.println("Exception thrown "
                               + "while passing null: " + e);
        }
    }
}

chevron_right


Output:

Exception thrown while passing null: java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html#compose-java.util.function.Function-

4. identity()

This method returns a function which 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 which returns its own argument

Below is the code to illustrate identity() method:

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.function.Function;
  
public class Main {
    public static void main(String args[])
    {
  
        // Function which takes in a number and
        // returns it
        Function i = Function.identity();
  
        System.out.println(i);
    }
}

chevron_right


Output:

java.util.function.Function$$Lambda$1/250421012@119d7047

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html#identity–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.