Thursday, July 23, 2015

Java Lambda - ToLongFunction Functional Interface

ToLongFunction Functional Interface

Before we begin to discuss about ToLongFunction 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.

ToLongFunction interface represents a function that accepts argument T and returns long valued result.


ToLongFunction Interface Declaration

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



applyAsLong() method

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

ToLongFunction<String> toLongFunc = (str) -> str.length();
long result = toLongFunc.applyAsLong("Monday");
System.out.println(result); //Output 6



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