Java : LongFunction with Examples

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


Summary

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

Class diagram

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

final var func = new LongFunction<String>() {
    @Override
    public String apply(long 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 = LongStream.of(5, 16, 255);

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

Methods

R apply (long value)

Applies this function to the given argument.

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

final var func = new LongFunction<String>() {
    @Override
    public String apply(long 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 = LongStream.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