What is the IntFunction functional interface in Java?

IntFunction is a functional interface in Java that accepts one argument of type int and returns a result R. The interface contains one method, i.e., apply.

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

import java.util.function.IntFunction;

Method

The apply(int value) method applies the function to the given argument of type int. This is the functional method of the interface.

Syntax

R apply(int value);

Parameter

  • int value: The function argument of type int.

Returns

  • R: The method returns the function result.

Code

import java.util.function.IntFunction;
public class Main{
public static void main(String[] args) {
IntFunction<String> stringIntFunction = (d) -> String.format("The passed value is %s", d);
int i = 100;
// calling apply method of the IntFunction
System.out.println(stringIntFunction.apply(i));
}
}

In the above code, we created an implementation of the IntFunction interface that accepts an int argument and returns a string.

Free Resources