Saturday, July 18, 2015

Java Lambda - DoubleToIntFunction Functional Interface

DoubleToIntFunction Functional Interface

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

DoubleToIntFunction interface represents a function that accepts a double-valued argument and produces int-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.

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

applyAsInt() method
this method applies this function to double argument and returns int value. We will take simple example that takes PI(3.14) as input and returns its floor value.

DoubleToIntFunction result = (pi) -> (int) Math.floor(pi);
double pi = Math.PI;
System.out.println(result.applyAsInt(pi));//Outputs 3


That’s all on DoubleToIntFunction 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