Tuesday, July 21, 2015

Java Lambda - IntFunction Functional Interface

IntFunction Functional Interface

IntFunction interface represents a function that accepts int valued argument or operand and produced result of type R. This is int 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.



IntFunction Interface Declaration

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



apply() method

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

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



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