How do I use the IntToLongFunction functional interface in Java?

The IntToLongFunction functional interface in Java is a specialized @FunctionalInterface introduced in Java 8 as part of the java.util.function package. It represents a function that takes a single int-valued argument and produces a long-valued result. This is intended to avoid boxing and unboxing of primitive values, improving performance when we need to work with these data types.

Key Characteristics of IntToLongFunction

  • Functional Method: The single abstract method in this interface is:
long applyAsLong(int value);
  • It takes an int as input and returns a long.
  • Annotation: It is annotated with @FunctionalInterface, meaning it can be used as a target for lambda expressions or method references.

How to Use IntToLongFunction

We can use the IntToLongFunction in several ways, such as with lambda expressions, method references, or by implementing the interface explicitly.

Example 1: Using Lambda Expressions

package org.kodejava.util.function;

import java.util.function.IntToLongFunction;

public class IntToLongFunctionExample {
   public static void main(String[] args) {
      // Example: Convert an int to its square and return long
      IntToLongFunction intToSquareLong = x -> (long) x * x;

      int input = 5;
      long result = intToSquareLong.applyAsLong(input);

      System.out.println("The square of " + input + " is: " + result);
   }
}

Example 2: Using Method References

If we already have a method that matches the applyAsLong signature, we can use a method reference:

package org.kodejava.util.function;

import java.util.function.IntToLongFunction;

public class IntToLongFunctionExample2 {
   public static void main(String[] args) {
      IntToLongFunction intToHexLong = Integer::toUnsignedLong;

      int input = -10;
      long result = intToHexLong.applyAsLong(input);

      System.out.println("The unsigned long value of " + input + " is: " + result);
   }
}

Example 3: Explicit Implementation of IntToLongFunction

We can explicitly implement the functional interface (though this is less common):

package org.kodejava.util.function;

import java.util.function.IntToLongFunction;

public class IntToLongFunctionExample3 {
   public static void main(String[] args) {
      IntToLongFunction intToDoubleLong = new IntToLongFunction() {
         @Override
         public long applyAsLong(int value) {
            return (long) value * 2;
         }
      };

      int input = 15;
      long result = intToDoubleLong.applyAsLong(input);

      System.out.println("Double of " + input + " is: " + result);
   }
}

Real-World Use Cases

Primitive Stream Processing:

IntToLongFunction can be used with IntStream.mapToLong() to process an IntStream and produce a LongStream.

package org.kodejava.util.function;

import java.util.stream.IntStream;

public class IntToLongStreamExample {
   public static void main(String[] args) {
      IntStream.range(1, 5)
              .mapToLong(x -> (long) x * x)
              .forEach(System.out::println); // Output: 1, 4, 9, 16
   }
}

Custom Transformation Logic:

Use it to convert input int data into a long result in scenarios such as file size calculations, timestamp conversions, or memory addresses. This example demonstrates how the IntToLongFunction can be used to implement custom transformation logic like time conversions, ensuring no boxing/unboxing overhead occurs.

package org.kodejava.util.function;

import java.util.function.IntToLongFunction;

public class CustomTransformationExample {
   public static void main(String[] args) {
      // Converts seconds (int) into milliseconds (long)
      IntToLongFunction secondsToMilliseconds = seconds -> (long) seconds * 1000;

      int seconds = 120; // 120 seconds = 2 minutes
      long milliseconds = secondsToMilliseconds.applyAsLong(seconds);

      System.out.println(seconds + " seconds is equal to " + milliseconds + " milliseconds.");
   }
}

Output:

120 seconds is equal to 120000 milliseconds.

Summary

The IntToLongFunction functional interface is highly useful when we need to work with int and long primitives while avoiding the overhead of boxing and unboxing. We can use it with lambda expressions, method references, or explicitly implement it based on the needs of our application.

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.