ToIntFunction Interface in Java with Examples

The ToIntFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in an argument of type T and produces an int-valued result.

This functional interface takes in only one generic, namely:-

  • T: denotes the type of the input argument to the operation

The lambda expression assigned to an object of ToIntFunction type is used to define its applyAsInt() which eventually applies the given operation on its only argument. It is similar to using an object of type Function<T, Integer>.

The ToIntFunction interface has only one function:

applyAsInt()

This method accepts an argument of type T and gives an int-valued result.

Syntax:

int applyAsInt(T value)

Parameters: This method takes in one parameter value which is an argument of type T.

Returns: This method returns an int-valued result.

Below is the code to illustrate applyAsInt() method:

Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.function.ToIntFunction;
  
public class Main {
    public static void main(String args[])
    {
  
        // Instantiating ToIntFunction
        ToIntFunction<Double> ob = a -> (int)(a * 10);
  
        // Applying the above function
        // using applyAsInt()
        System.out.println(ob.applyAsInt(3.2));
    }
}

chevron_right


Output:

32


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.