Trusted answers to developer questions

What is the ToIntFunction functional interface in Java?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

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

RELATED TAGS

java
functional
tointfunction
Did you find this helpful?