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));
}
}
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources