Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

What is DoubleFunction functional interface in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring