Java : DoubleFunction with Examples

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


Summary

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

Class diagram

final var stream = DoubleStream.of(0.1, 0.2, 0.3);

final var func = new DoubleFunction<String>() {
    @Override
    public String apply(double value) {
        return "num=" + value;
    }
};

final var ret = stream.mapToObj(func).toList();
System.out.println(ret); // [num=0.1, num=0.2, num=0.3]
// An example with a lambda expression.
final var stream = DoubleStream.of(0.1, 0.2, 0.3);

final var ret = stream.mapToObj(value -> "num=" + value).toList();
System.out.println(ret); // [num=0.1, num=0.2, num=0.3]

Methods

R apply (double value)

Applies this function to the given argument.

final var stream = DoubleStream.of(0.1, 0.2, 0.3);

final var func = new DoubleFunction<String>() {
    @Override
    public String apply(double value) {
        return "num=" + value;
    }
};

final var ret = stream.mapToObj(func).toList();
System.out.println(ret); // [num=0.1, num=0.2, num=0.3]
// An example with a lambda expression.
final var stream = DoubleStream.of(0.1, 0.2, 0.3);

final var ret = stream.mapToObj(value -> "num=" + value).toList();
System.out.println(ret); // [num=0.1, num=0.2, num=0.3]

Related posts

To top of page