Thursday, July 23, 2015

Java Lambda - ToDoubleFunction Functional Interface

ToDoubleFunction Functional Interface

Before we begin to discuss about ToDoubleFunction interface we need to know about Function interface. Function interface takes argument T as input and return result R. BiFunction is two-arity version of Function interface where it can take two different arguments T and U as input and returns result as R. I would highly recommend you to read it. I also have written on higher order functions.

ToDoubleFunction interface represents a function that accepts argument T and returns double valued result.


ToDoubleFunction Interface Declaration

public interface ToDoubleFunction<T>
ToDoubleFunction interface represents a function that accepts input of argument T and returns double valued result. This is double producing primitive specialization of Function interface.



applyAsDouble() method

double applyAsDouble(T value);
T is the input for the method applyAsDouble(). This function applies the given argument and returns double result. Let us take simple example of accepting a string and return its length.

ToDoubleFunction<String> toDoubleFunc = (str) -> str.length();
double result = toDoubleFunc.applyAsDouble("Monday");
System.out.println(result); //Output 6.0



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