Saturday, July 18, 2015

Java Lambda - DoubleToLongFunction Functional Interface

DoubleToLongFunction Functional Interface

We have discussed Function interface and how to write higher order functions using Function interface. I highly recommend that you read those first before continuing to read this.

DoubleToLongFunction interface represents a function that accepts a double-valued argument and produces long-valued result. Function interface accepts the input of any type T and returns result of any type R. We cannot use primitive data types in Function interface as there is no support for primitives in Generics.

DoubleToLongFunction is a specialized version of Function interface for input as double and output as long. DoubleToLongFunction is used to accept the input type double and return long.

applyAsLong() method

This method applies this function to double argument and returns long value. We will take simple example that takes double as input and casts that value to long and return it.

DoubleToLongFunction result = (value) -> (long)value;
long result.applyAsLong(3.5d);
System.out.println(l); //Outputs 3


That’s all on DoubleToLongFunction interface.

Read about important java.util.function package’s interface here. Consumer, Function, Supplier, BinaryOperator & Predicate Functional Interfaces. I have also written on High Order functions using Function functional interface.



No comments:

Post a Comment

Ads Inside Post