What is DoubleFunction functional interface in Java?
Overview
DoubleFunction is a functional interface that accepts one argument of type double and returns back a result. The interface contains one method, i.e, apply.
The DoubleFunction interface is defined in the java.util.function package. To import the DoubleFunction interface, check the following import statement.
import java.util.function.DoubleFunction;
apply(double value)
This method applies the function to the given argument of type double. This is the functional method of the interface.
Syntax
The syntax of the apply method is given below.
R apply(double value);
Parameters
double value: The function argument of typedouble.R: The return type of the function.
Returns
The method returns a value of type R.
Code
import java.util.function.DoubleFunction;public class Main{public static void main(String[] args) {DoubleFunction<String> stringDoubleFunction = (d) -> String.format("The passed value is %s", d);double d = 100.3;// calling apply method of the DoubleFunctionSystem.out.println(stringDoubleFunction.apply(d));}}
Explanation
- Line 1: We import the relevant packages.
- Line 5: We create an implementation of the
DoubleFunctioninterface that accepts a double argument and returns a string. - Line 6: We define a
doublevalue calledd. - Line 9: We call the
apply()method withdas the argument and print the result to the console.