LongFunction Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expects an long-valued argument as an input and produces a result. As it is a functional interface, it can be used assignment target for lambda expression or method reference. In this post, we are going to see several implementations of LongFunction Interface by using different examples.

LongFunction Interface in Java 8 with Examples...!!! Share on X
Look at LongFunction Javadoc description below:

LongFunction Interface contains below method:
apply
Let’s discuss this method:
apply
This method performs operation on the given long-valued argument and return a result.
R apply(long value);
Lets understand above mentioned method through below example:
Example. ‘apply’ method
import java.util.function.LongFunction;
public class LongFunctionInterfaceJava8Example {
public static void main(String[] args) {
System.out.println("LongFunction Interface - 'apply' example \n");
LongFunction <Integer> longFuncObj1 = i -> (int) i * 2;
LongFunction <Boolean> longFuncObj2 = i -> i > 2;
System.out.println("Double of 5: " + longFuncObj1.apply(5));
System.out.println("Is number 2 smaller than 5?: " + longFuncObj2.apply(5));
}
}
Output:
LongFunction Interface - 'apply' example Double of 5: 10 Is number 2 smaller than 5?: true
Java 8 LongFunction Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.
LongFunction Interface in Java 8 with Examples...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Supplier Interface in Java 8 with Examples