What is ToDoubleFunction functional interface in Java?

ToDoubleFunction is a functional interface that accepts one argument and returns back a result of type double. The interface contains one method applyAsDouble().

The ToDoubleFunction interface is defined in the java.util.function package. To import the ToDoubleFunction interface check the following import statement.

import java.util.function.ToDoubleFunction;

The applyAsDouble(T value) method applies the function to the given argument and returns the result of type double. This is the functional method of the interface.

Syntax

double applyAsDouble(T value);

Parameters

  • T value: The function argument.

Returns

The method returns the result of type double.

Code

import java.util.function.ToDoubleFunction;
public class Main{
public static void main(String[] args) {
ToDoubleFunction<Integer> convertSumToDouble = (i1) -> i1 + 5;
Integer i1 = 5;
// calling applyAsDouble method of the ToDoubleFunction
System.out.println(convertSumToDouble.applyAsDouble(i1));
}
}

In the code above, we create a ToDoubleFunction interface that accepts an integer argument and adds the integer 5 to the passed argument returning the result as a double value.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources