Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
functional
function

What is the Function functional interface in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Function is a functional interface that accepts one argument and returns a result. The apply() method of the interface is the functional method.

The Function interface is defined in the java.util.function package. To import the Function interface, check the following import statement:

import java.util.function.Function;

Syntax


R apply(T t);

Parameters

  • T t: This is the function argument.

Return value

The method returns the function result.

Code

import java.util.function.Function;
public class Main {
public static void main(String[] args){
Function<Integer, Integer> addFunction = arg -> arg + 5;
int arg = 1;
System.out.println("Result of apply() on addFunction --> " + addFunction.apply(arg));
}
}
Implementation of the apply() method

Explanation

  • Line 1: We import the Function interface from the function package.
  • Line 6: We define an implementation of the Function interface called addFunction. addFunction accepts an integer and returns the result of adding 5 to the given integer.
  • Line 7: We define an integer called arg.
  • Line 8: We print the result of using the apply() method and pass arg as an argument.

RELATED TAGS

java
communitycreator
functional
function

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring