LongToIntFunction interface in Java


A LongToIntFunction in Java is a functional interface which takes one argument of type long and returns a result of int type. Functional interface is an interface that have only one abstract method. To use this interface first you have to import java.util.function package.

The functional method of this interface is ‘applyAsInt()’ which takes long value as an argument and returns int valued result.

Syntax

int applyAsInt(long value);

Algorithm

To implement this functional interface, follow these steps −

  • 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 instance of the functional interface, passing in a long value as input.

  • Step 3 − Use the output of the applyAsInt(long value) method in your code as needed.

Approach 1: Define a Lamda 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);
   }
}

Output

25

Explanation

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

Approach 2: Using a Method Reference

You can use a method reference to implement the applyAsInt(long value) method of the 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);
   }
}

Output

31

Explanation

we create a LongToIntFunction instance named digitCount using a method reference to the valueOf method of the String class. To arrive at the desired outcome, this technique involves converting the long value input to a String representation followed by computing and returning its length as an integer. We then call the applyAsInt method on the digitCount instance with the input long value of 9876543210L, which returns the number of digits in the long value as an int. The result is printed to the console.

Approach 3: Using Anonymous Inner Class

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);
   }
}

Output

27

Explanation

In this example, the LongToIntFunction instance digitCount returns the number of digits in the input long value of 9876543210L, which is 10. The result is printed to the console.

Comparison Between Approaches

Criteria

Approach 1

Approach 2

Approch 3

Type

Lamda expression

Method reference

Anonymous Inner class

Method

applyAsInt(long value)

applyAsInt(long value)

applyAsInt(long value)

Method Logic

Returns an int value

Returns an int value

Returns an int value

Conclusion

By using these approaches, you can work with the LongAsIntFunction interface in a variety of ways to implement custom functions that take long values as input and return results of different types.

Updated on: 28-Jul-2023

25 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements