How do I use the IntFunction functional interface in Java?

The IntFunction functional interface in Java is part of the java.util.function package. It represents a function that takes an int argument and produces a result. This interface is useful when dealing with primitive int inputs to avoid autoboxing overhead that comes with using Function for integers.

Here’s a breakdown of how to use IntFunction:


1. Functional Interface Definition

The IntFunction interface is defined as:

@FunctionalInterface
public interface IntFunction<R> {
    R apply(int value);
}
  • Method: R apply(int value)
    • Takes an int as input.
    • Produces a result of type R (generic return type).

2. How to Use IntFunction

We can implement the IntFunction interface using:
Lambda expressions.
Method references.
Anonymous classes.

Example 2.1: Using a Lambda Expression

package org.kodejava.util.function;

import java.util.function.IntFunction;

public class IntFunctionExample {
  public static void main(String[] args) {
    // Define an IntFunction to convert an int to a String
    IntFunction<String> intToString = value -> "Value: " + value;

    // Use the IntFunction
    String result = intToString.apply(42);
    // Output: Value: 42
    System.out.println(result);
  }
}

Example 2.2: Using a Method Reference

package org.kodejava.util.function;

import java.util.function.IntFunction;

public class IntFunctionMethodReferenceExample {
  public static void main(String[] args) {
    // Use Integer.toString(int) as an IntFunction
    IntFunction<String> intToString = Integer::toString;

    // Apply the function
    String result = intToString.apply(100);
    // Output: 100
    System.out.println(result);
  }
}

Example 2.3: Anonymous Class Implementation

package org.kodejava.util.function;

import java.util.function.IntFunction;

public class IntFunctionAnonymousExample {
  public static void main(String[] args) {
    // Anonymous class implementation
    IntFunction<Double> intToDouble = new IntFunction<Double>() {
      @Override
      public Double apply(int value) {
        return value * 2.5;
      }
    };

    // Use the function
    Double result = intToDouble.apply(4);
    // Output: 10.0
    System.out.println(result);
  }
}

3. Using IntFunction in Streams

The IntFunction interface is commonly used in situations like transforming streams of primitive values (IntStream).

Example 3.1: Transforming an IntStream Using IntFunction

package org.kodejava.util.function;

import java.util.function.IntFunction;
import java.util.stream.IntStream;

public class StreamIntFunctionExample {
  public static void main(String[] args) {
    IntFunction<String> intToWord = value -> "Number: " + value;

    // Apply the IntFunction in a Stream
    IntStream.range(1, 5)
            .mapToObj(intToWord)  // Convert each int to a String
            .forEach(System.out::println);

    // Output:
    // Number: 1
    // Number: 2
    // Number: 3
    // Number: 4
  }
}

4. Key Benefits

  • Avoids autoboxing when working with primitive int values.
  • Great for functional programming paradigms.
  • Simplifies transforming or processing int values in streams.

Summary

  • IntFunction represents a function that takes an int and returns a result of type R.
  • Use lambdas, method references, or anonymous classes to implement it.
  • Commonly used in streams or when processing primitives directly without using Function<Integer, R> to avoid autoboxing overhead.

Leave a Reply

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