ToDoubleFunction Interface in Java with Examples - GeeksforGeeks

ToDoubleFunction Interface in Java with Examples

Last Updated : 11 Sep, 2026

The ToDoubleFunction is a functional interface from the java.util.function package introduced in Java 8. It represents a function that accepts an argument of type T and produces a double result.

  • T represents the type of the input argument.
  • The interface uses the applyAsDouble() method to perform the defined operation.
  • It is similar to using Function<T, Double>, but it directly produces a primitive double result.

Example: Program demonstrates how ToDoubleFunction<Integer> converts an integer value into a double result by calculating its square root.

Java
import java.util.function.ToDoubleFunction;

public class GFG{
    public static void main(String[] args)
    {

        ToDoubleFunction<Integer> operation = value -> Math.sqrt(value);

        System.out.println(operation.applyAsDouble(25));
    }
}

Output
5.0

Explanation: The ToDoubleFunction<Integer> accepts 25 as an Integer value. The lambda expression calculates its square root using Math.sqrt(), and applyAsDouble() returns the result as a primitive double.

Syntax of ToDoubleFunction

A ToDoubleFunction can be declared as:

ToDoubleFunction<T> function;

Here, T specifies the type of input accepted by the function. Regardless of the input type, the result is always a primitive double.

Its functional method has the following form:

double applyAsDouble(T value);

Working of ToDoubleFunction

This flow demonstrates how an input of type T is processed by ToDoubleFunction to produce a primitive double result.

  • A value of type T is passed to applyAsDouble().
  • The value is received by the lambda expression.
  • The lambda expression performs the specified operation.
  • The calculated value is returned as a primitive double.

Method of ToDoubleFunction

The ToDoubleFunction interface contains one abstract method, applyAsDouble().

applyAsDouble()

The applyAsDouble() method executes the operation defined by the ToDoubleFunction and returns the calculated value as a primitive double.

Syntax

double applyAsDouble(T value)

  • Parameter: value: The input argument of type T on which the operation is performed.
  • Return Type: A primitive double representing the result of the operation.

Example: Program uses ToDoubleFunction<String> to calculate the length of a string and return it as a double.

Java
import java.util.function.ToDoubleFunction;

public class GFG {
    public static void main(String[] args) {

        ToDoubleFunction<String> operation =
            value -> value.length();

        System.out.println(operation.applyAsDouble("Geeks"));
    }
}

Output
5.0

Explanation: Here, T is String, so the function accepts a string as input. The lambda expression calculates its length, and applyAsDouble() returns the length as a primitive double.

ToDoubleFunction vs Function<T, Double>

Both interfaces can be used to transform an object into a double value. The main difference is how the result is represented.

FeatureToDoubleFunction<T>Function<T, Double>
InputTT
OutputPrimitive doubleDouble object
Abstract methodapplyAsDouble()apply()
Boxing of resultNot requiredMay be required
Primitive specializationYesNo
Comment