What is the ToIntFunction functional interface in Java?

ToIntFunction is a functional interface that accepts one argument and returns an int type result. The interface contains one method, i.e., applyAsInt.

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

import java.util.function.ToIntFunction;

applyAsInt(T value)

This method applies the function to the given argument and returns the int type result. This is the functional method of the interface.

Syntax

int applyAsInt(T value);

Parameters

  • T value: This is the function argument.

Returns

The method returns the result of type int.

Code

import java.util.function.ToIntFunction;
public class Main{
public static void main(String[] args) {
ToIntFunction<Integer> convertSumToInt = (i1) -> i1 + 5;
Integer i1 = 5;
// calling applyAsInt method of the ToIntFunction
System.out.println(convertSumToInt.applyAsInt(i1));
}
}

Free Resources