How do I use the IntToDoubleFunction functional interface in Java?

The IntToDoubleFunction is a functional interface in Java that belongs to the java.util.function package. It represents a function that takes a single int argument and produces a double result. It’s particularly useful when working with streams or when we need to define lightweight logic for converting an int to a double.

Functional Method

  • The functional method of the IntToDoubleFunction is applyAsDouble(int value), which we must implement or provide a lambda for.

Where to Use

  • When processing streams of integers using the IntStream API.
  • When we need to convert an int to a double without unnecessary boxing and unboxing.
  • When working in functional programming contexts or defining transformations.

Example Usage

1. Using a Lambda Expression

We can use a lambda expression to define the logic of converting an int to a double.

package org.kodejava.util.function;

import java.util.function.IntToDoubleFunction;

public class IntToDoubleFunctionExample {
    public static void main(String[] args) {
        // Define a lambda to convert int to double
        IntToDoubleFunction doubleHalf = (int value) -> value / 2.0;

        // Test the function
        int input = 10;
        double result = doubleHalf.applyAsDouble(input);
        System.out.println("Half of " + input + " is " + result);
    }
}

Output:

Half of 10 is 5.0

2. Using with IntStream

We can commonly use IntToDoubleFunction with IntStream, especially when mapping a stream of integers to a stream of doubles.

package org.kodejava.util.function;

import java.util.stream.IntStream;

public class IntStreamExample2 {
    public static void main(String[] args) {
        // IntStream with mapping using IntToDoubleFunction
        IntStream.range(1, 5) // Stream of ints: 1, 2, 3, 4
                .mapToDouble(value -> value * 1.5) // Map int to double
                .forEach(System.out::println); // Print the results
    }
}

Output:

1.5
3.0
4.5
6.0

3. Using Method References

If we have existing methods that fulfill the IntToDoubleFunction signature (accepting an int and returning a double), we can use method references.

package org.kodejava.util.function;

import java.util.function.IntToDoubleFunction;

public class MethodReferenceExample2 {
    public static void main(String[] args) {
        IntToDoubleFunction squareRoot = Math::sqrt;

        int input = 16;
        double result = squareRoot.applyAsDouble(input);
        System.out.println("Square root of " + input + " is " + result);
    }
}

Output:

Square root of 16 is 4.0

Key Points:

  1. The IntToDoubleFunction avoids boxing/unboxing by working with primitives (int and double).
  2. It is typically used with functional programming patterns such as streams or creating lightweight transformations.
  3. We can use lambdas, method references, or any class that implements the functional interface.

This makes it a convenient interface for operations involving primitive int to double transformations.

Wayan

Leave a Reply

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