Java 8 DoubleToLongFunction Example
In this blog post, I will be explaining how the Java 8 functional interface DoubleToLongFunction works. To know more about functional interfaces, you can refer this blog post.
What is DoubleToLongFunction
The DoubleToLongFunction interface is a specialization of the Function interface. While the Function interface accepts any data type, the DoubleToLongFunction interface accepts as parameter a double value and returns a result of type long. To see an example of the Function interface, refer to this blog post.
The DoubleToLongFunction interface provides a method called applyAsLong. It accepts a single parameter of double data type. It returns a result of long data type. So it basically applies the logic in the applyAsLong method to the input parameter and returns the result.
DoubleToLongFunction Example Code
The following code snippet demonstrates the DoubleToLongFunction interface:
DoubleToLongFunction longRetriever = input -> Double.valueOf(input).longValue();Double input = 5.6;System.out.println("Long Value "+longRetriever.applyAsLong(input) );
Here, we have written a DoubleToLongFunction implementation that returns the Integer part of the input number as a long. The DoubleToLongFunction.applyAsLong method is implemented using a lambda expression. So when you execute this code, it will print the following output:
Long Value 5
Conclusion
The DoubleToLongFunction is a specialization of the Function interface. It accepts as parameter a Double input and returns a Long output.
Comments
Post a Comment