Java 8 DoubleToIntFunction Example

In this blog post, I will be explaining how the Java 8 functional interface DoubleToIntFunction works. To know more about functional interfaces, you can refer this blog post.

Edit

What is DoubleToIntFunction

The DoubleToIntFunction  interface is a specialization of the Function interface. While the Function interface accepts any data type, the DoubleToIntFunction  interface accepts as parameter a double value and returns a result of type int. To see an example of the Function interface, refer to this blog post.

 

The DoubleToIntFunction interface provides a method called applyAsInt. It accepts a single parameter of double data type. It returns a result of int data type. So it basically applies the logic in the applyAsInt method to the input parameter and returns the result.

DoubleToIntFunction Interface Example Code

The following code snippet demonstrates the DoubleToIntFunction interface:

DoubleToIntFunction integerRetriever = input -> Double.valueOf(input).intValue();Double input = 5.6;System.out.println("Integer Value "+integerRetriever.applyAsInt(input) );

 

Here, we have written a DoubleToIntFunction implementation that returns the Integer part of the input number. The DoubleToIntFunction.applyAsInt method is implemented using a lambda expression. So when you execute this code, it will print the following output:

Integer Value 5

Conclusion

The DoubleToIntFunction is a specialization of the Function interface. It accepts as parameter a Double input and returns an Integer output.

Comments

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set