LongFunction in Java is a functional interface that accepts a parameter of type long and returns a result of some other type. A functional interface is an interface with only one abstract method. To first use this interface, you must import the java.util.function package.
The function method of this interface is "apply()", which takes a long value as a parameter and returns the function result represented by R.
R apply(long value);
In order to effectively implement this functional interface in your work, there are three definition steps that must be followed beforehand.
First, setting up an instance of such an implementation requires defining a suitable lambda expression that can accurately preview what will be used in its "apply" function when a long type variable or argument is provided.
After setting up a specific algorithm via step 1 here, proceed to step 2 - calling "apply" on said instance while adding the long type argument inputs respectively. Once steps 1 and 2 have been satisfactorily executed, it's time for our third and final step - to use all the output values from the previous steps within the available area in our ongoing code.
You can define a lambda expression to implement the apply(long value) method of the LongFunction
import java.util.function.LongFunction; public class LongFunctionExample { public static void main(String[] args) { LongFunction<String> longToString = l -> Long.toString(l); String result = longToString.apply(123L); System.out.println(result); } }
123
This code creates a LongFunction
You can use method references to implement the apply(long value) method of LongFunction
import java.util.function.LongFunction; public class LongFunctionExample { public static void main(String[] args) { LongFunction<Integer> countDigits = Long::toString().length; int result = countDigits.apply(12345L); System.out.println(result); } }
12345
Creates a LongFunction
To use a predefined function that implements the LongFunction
import java.util.function.LongFunction; public class LongFunctionExample { public static void main(String[] args) { LongFunction<Double> sqrt = Math::sqrt; double result = sqrt.apply(16L); System.out.println(result); } }
4.0
In the above sqrt predefined function, LongFunction
standard |
method 1 |
Method 2 |
Method 3 |
---|---|---|---|
type |
Lamda expression |
Method reference |
Predefined functions |
method |
Application (long value) |
Application (long value) |
Application (long value) |
Method logic |
Return long value |
Return long value |
Return long value |
There are several ways to help you use the LongFunction
The above is the detailed content of LongFunction interface in Java. For more information, please follow other related articles on the PHP Chinese website!