Wednesday, July 22, 2015

Java Lambda - LongFunction Functional Interface

LongFunction Functional Interface

LongFunction interface represents a function that accepts long valued argument or operand and produced result of type R. This is long data type (primitive) specialization for Function interface.

We have discussed Function, BiFunction, DoubleFunction, DoubleToIntFunction, DoubleToLongFunction interfaces. I would highly recommend reading them. I have also written on High Order functions using Function functional interface.



LongFunction Interface Declaration

public interface LongFunction<R>
LongFunction interface accepts the object type of R which indicates the result.



apply() method

R apply(long value);
This method is used to apply this function to given argument and return the result. Below example shows us how to use LongFunction interface. It accepts the value as long data type and return the string version of it. Then we take the length of that result.

LongFunction<String> longFunction = (val) -> String.valueOf(val);
System.out.println(longFunction.apply(10).length()); //Outputs 2



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