Tuesday, July 14, 2015

Java Lambda - DoubleFunction Functional Interface

DoubleFunction Functional Interface

We have discussed Function interface previously. I would highly recommend you to read about Function interface before reading DoubleFunction. Function interface has a generic type T as input and returns result of type R, while DoubleFunction is a specialized version of Function for double.

DoubleFunction represents a function that accepts a double argument and produces a result. This is a primitive specialized version of Function interface.

DoubleFunction interface declaration

public interface DoubleFunction<R>
DoubleFunction interface with type R.


apply() method

R apply(double value);
apply() method is used to apply this function to give argument.
Let us take simple example of apply() method i.e. convert double to String.

DoubleFunction<String> doubleFunction = (x) -> String.valueOf(x) + " converted to String";
String str = doubleFunction.apply(34d);
System.out.println(str);

The above code generates following result:
34.0 converted to String

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