Java 8 DoubleFunction Example

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

Edit

The  DoubleFunction interface provides a method called apply. It accepts a single parameter of double data type. It returns a result of any data type. So it basically applies the logic in the apply method to the input parameter and returns the result. The DoubleFunction interface is a specialization of the Function interface. While the Function interface accepts any data type, the DoubleFunction interface accepts a long value. To see an example of the Function interface, refer to this blog post.

DoubleFunction Interface that returns an Integer

Consider the following code snippet:

DoubleFunction<Integer> integerRetriever = input -> new Double(input).intValue();Double input = 4.8;System.out.println("Integer Value "+integerRetriever.apply(input) );

 

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

Integer Value 4

You can get the source code for this example along with the code for other Java 8 examples at the Github repository here.

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