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: know more java training

Program 1:

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));

}

}

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.know more java training

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:

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));

}

}

Output:

15.0

Program 2: To demonstrate when NullPointerException is returned.

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);

}

}

}

Output:

Exception thrown while passing null: java.lang.NullPointerException 

know more java training

Comments

Popular posts from this blog

Salesforce Signs Definitive Agreement to Acquire Slack

Salesforce Signs Definitive Agreement to Acquire Slack

Linux Interview Questions