Home > Java > javaTutorial > body text

LongToIntFunction interface in Java

王林
Release: 2023-09-07 16:29:16
forward
726 people have browsed it

LongToIntFunction interface in Java

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.

grammar

int applyAsInt(long value);
Copy after login

algorithm

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.

Method 1: Define Lambda expression

You can define a lambda expression to implement the apply(long value) method of the LongToIntFunction interface.

Example

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);
   }
}
Copy after login

Output

25
Copy after login

illustrate

LongToIntFunction instance square returns the square of the input long value 5L, which is 25.

Method 2: Using method references

You can use method references to implement the applyAsInt(long value) method of LongToIntFunction.

Example

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);
   }
}
Copy after login

Output

31
Copy after login

illustrate

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.

Method 3: Use anonymous inner classes

Example

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);
   }
}
Copy after login

Output

27
Copy after login

illustrate

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.

Comparison between methods

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

in conclusion

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!