Trusted answers to developer questions

What is DoubleFunction functional interface in Java?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

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 type double.
  • 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 DoubleFunction
System.out.println(stringDoubleFunction.apply(d));
}
}

Explanation

  • Line 1: We import the relevant packages.
  • Line 5: We create an implementation of the DoubleFunction interface that accepts a double argument and returns a string.
  • Line 6: We define a double value called d.
  • Line 9: We call the apply() method with d as the argument and print the result to the console.

RELATED TAGS

Did you find this helpful?