Java : IntFunction with Examples

IntFunction (Java SE 21 & JDK 21) with Examples.
You will find code examples on most IntFunction<R> methods.


Summary

Represents a function that accepts an int-valued argument and produces a result. This is the int-consuming primitive specialization for Function.

Class diagram

final var stream = IntStream.of(5, 16, 255);

final var func = new IntFunction<String>() {
    @Override
    public String apply(int value) {
        return "%#x".formatted(value);
    }
};

final var ret = stream.mapToObj(func).toList();
System.out.println(ret); // [0x5, 0x10, 0xff]
// An example with a lambda expression.
final var stream = IntStream.of(5, 16, 255);

final var ret = stream.mapToObj(value -> "%#x".formatted(value)).toList();
System.out.println(ret); // [0x5, 0x10, 0xff]

Methods

R apply (int value)

Applies this function to the given argument.

final var stream = IntStream.of(5, 16, 255);

final var func = new IntFunction<String>() {
    @Override
    public String apply(int value) {
        return "%#x".formatted(value);
    }
};

final var ret = stream.mapToObj(func).toList();
System.out.println(ret); // [0x5, 0x10, 0xff]
// An example with a lambda expression.
final var stream = IntStream.of(5, 16, 255);

final var ret = stream.mapToObj(value -> "%#x".formatted(value)).toList();
System.out.println(ret); // [0x5, 0x10, 0xff]

Related posts

To top of page