Java : ToIntFunction with Examples
ToIntFunction (Java SE 21 & JDK 21) with Examples.
You will find code examples on most ToIntFunction<T> methods.
Summary
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)
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]