LongToIntFunction in Java is a functional interface that accepts a long type parameter and returns an int type result. 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 functional method of this interface is "applyAsInt()", which takes a long value as a parameter and returns an int value result.
int applyAsInt(long value);
To implement this functional interface, please follow the steps below -
Step 1 - Create an instance of the functional interface by defining a lambda expression that implements the applyAsInt(long value) method.
Step 2 - Call the applyAsInt(long value) method on the functional interface instance, passing in a long value as input.
Step 3 - Use the output of the applyAsInt(long value) method in your code as needed.
You can define a lambda expression to implement the apply(long value) method of the LongToIntFunction interface.
import java.util.function.LongToIntFunction; public class LambdaExample { public static void main(String[] args) { LongToIntFunction square = value -> (int) (value * value); int result = square.applyAsInt(5L); System.out.println(result); } }
25
LongToIntFunction instance square returns the square of the input long value 5L, which is 25.
You can use method references to implement the applyAsInt(long value) method of LongToIntFunction.
import java.util.function.LongToIntFunction; public class MethodReferenceExample { public static void main(String[] args) { LongToIntFunction digitCount = String::valueOf; int result = digitCount.applyAsInt(9876543210L); System.out.println(result); } }
31
We create a LongToIntFunction instance named digitalCount using a method reference to the valueOf method of the String class. To achieve the desired result, this technique involves converting a long value input into a string representation, then calculating its length and returning it as an integer. We then call the applyAsInt method on the digitalCount instance with the input long value 9876543210L, which returns the number of digits in the long value as an int. The results are printed to the console.
import java.util.function.LongToIntFunction; public class AnonymousClassExample { public static void main(String[] args) { LongToIntFunction binaryLength = new LongToIntFunction() { @Override public int applyAsInt(long value) { return Long.toBinaryString(value).length(); } }; int result = binaryLength.applyAsInt(123456789L); System.out.println(result); } }
27
In this example, the LongToIntFunction instance digitCount returns the number of digits in the input long value 9876543210L, which is 10. The results are printed to the console.
standard |
method 1 |
Method 2 |
Method 3 |
---|---|---|---|
type |
Lamda expression |
Method reference |
Anonymous inner class |
method |
applyAsInt(long value) |
applyAsInt(long value) |
applyAsInt(long value) |
Method logic |
Return an int value |
Return an int value |
Return an int value |
By using these methods, you can use the LongAsIntFunction interface in a variety of ways to implement custom functions that take long values as input and return different types of results.
The above is the detailed content of LongToIntFunction interface in Java. For more information, please follow other related articles on the PHP Chinese website!