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...!!! Click To Tweet
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.
1 | R apply(long value); |
Lets understand above mentioned method through below example:
Example. ‘apply’ method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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)); } } |
1 2 3 4 | LongFunction Interface - 'apply' example Double of 5: 10 Is number 2 smaller than 5?: true |
LongFunction Interface in Java 8 with Examples...!!! Click To Tweet
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