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: The double valued 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 DoubleUnaryOperator interface from the function package.
  • Line 6: We define an implementation of the DoubleUnaryOperator interface called squareOfNumFunction. squareOfNumFunction accepts a double-valued argument and returns the result square of the given argument.
  • Line 7: We define a double value called arg.
  • Line 8: We print the result of using the applyAsDouble() method and pass arg as an argument.

Free Resources