What is the DoubleUnaryOperator functional interface in Java?
Overview
DoubleUnaryOperator is a functional interface that represents an operation on a single double-valued argument and produces a double-valued result. The applyAsDouble() method is the functional method of the interface.
The DoubleUnaryOperator interface is defined in the java.util.function package. To import the DoubleUnaryOperator interface check the following import statement.
import java.util.function.DoubleUnaryOperator;
Syntax
double applyAsDouble(double operand);
Parameters
double operand: Thedoublevalued operand.
Return value
This method returns a double valued result.
Code
import java.util.function.DoubleUnaryOperator;public class Main {public static void main(String[] args){DoubleUnaryOperator squareOfNumFunction = num -> num * num;double arg = 5.43;System.out.println("Result of applyAsDouble() on squareOfNumFunction --> " + squareOfNumFunction.applyAsDouble(arg));}}
Explanation
- Line 1: We import the
DoubleUnaryOperatorinterface from thefunctionpackage. - Line 6: We define an implementation of the
DoubleUnaryOperatorinterface calledsquareOfNumFunction.squareOfNumFunctionaccepts a double-valued argument and returns the result square of the given argument. - Line 7: We define a
doublevalue calledarg. - Line 8: We print the result of using the
applyAsDouble()method and passargas an argument.