Java : ToIntFunction with Examples

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


Summary

Represents a function that produces an int-valued result. This is the int-producing primitive specialization for Function.

Class diagram

final var stream = Stream.of("0x1", "0xa", "0xff");

final var func = new ToIntFunction<String>() {
    @Override
    public int applyAsInt(String value) {
        return Integer.decode(value) * 10;
    }
};

final var ret = stream.mapToInt(func).toArray();
System.out.println(Arrays.toString(ret)); // [10, 100, 2550]
// An example with a lambda expression.
final var stream = Stream.of("0x1", "0xa", "0xff");

final var ret = stream.mapToInt(value -> {
    return Integer.decode(value) * 10;
}).toArray();
System.out.println(Arrays.toString(ret)); // [10, 100, 2550]

Methods

int applyAsInt (T value)

Applies this function to the given argument.

final var stream = Stream.of("0x1", "0xa", "0xff");

final var func = new ToIntFunction<String>() {
    @Override
    public int applyAsInt(String value) {
        return Integer.decode(value) * 10;
    }
};

final var ret = stream.mapToInt(func).toArray();
System.out.println(Arrays.toString(ret)); // [10, 100, 2550]
// An example with a lambda expression.
final var stream = Stream.of("0x1", "0xa", "0xff");

final var ret = stream.mapToInt(value -> {
    return Integer.decode(value) * 10;
}).toArray();
System.out.println(Arrays.toString(ret)); // [10, 100, 2550]

Related posts

To top of page