DoubleUnaryOperator Interface in Java



In the world of Java programming, interfaces assume an urgent part in characterizing gets that classes should stick to. One such interface point is the DoubleUnaryOperator interface, which was presented in Java 8 as a piece of the functional programming improvements. This interface point addresses a procedure on a single double-valued operand that delivers a double-valued result. In this article, we will explore the syntax, use, and different ways to deal with carrying out the DoubleUnaryOperator interface in Java.

Syntax

The syntax for the DoubleUnaryOperator interface is as per follows ?

@FunctionalInterface
public interface DoubleUnaryOperator {
   double applyAsDouble(double operand);
}

Explanation of Syntax

Let's break down the syntax ?

  • The '@FunctionalInterface' explanation demonstrates that the interface is a functional interface , and that implies it tends to be utilized with lambda expressions.

  • The point of interaction name, 'DoubleUnaryOperator', mirrors its motivation of performing procedure on a single double value.

  • The 'applyAsDouble' technique is the main unique strategy proclaimed in the interface. It takes a double value as an input and returns a double value as the result of the operation.

Code of the Syntax

To all the more likely figure out the use of the DoubleUnaryOperator interface, we should take a look at a simple example ?

Example

import java.util.function.DoubleUnaryOperator;

public class SyntaxCode {
   public static void main(String[] args) {
      DoubleUnaryOperator operator = new DoubleUnaryOperator() {
         @Override
         public double applyAsDouble(double operand) {
            // Perform operation on the operand
            return operand * 2;
         }
      };

      double result = operator.applyAsDouble(5.0);
      System.out.println(result); 
   }
}

Output

10.0

Explanation of the Code

  • We import the expected class java.util.function.DoubleUnaryOperator for using the DoubleUnaryOperator interface.

  • We make an unknown class that executes the DoubleUnaryOperator interface, permitting us to characterize the way of behaving of the applyAsDouble technique straightforwardly inside the code.

  • Inside the anonymous class, we override the applyAsDouble method, which performs the operation of doubling the operand by multiplying it by 2.

  • We create an instance of the anonymous class using the new keyword, representing our implementation of the DoubleUnaryOperator interface.

  • Finally, we apply the DoubleUnaryOperator to the value 5.0 by calling the applyAsDouble method

Algorithm

To implement the DoubleUnaryOperator interface, we can follow these step-by-step guidelines ?

  • Characterize a lambda expression or a strategy that takes a double value as an input.

  • Perform the desired operation on the input value.

  • Return the result as a double value.

Approach 1: Lambda expression for squaring a double value.

In this approach, we will create a lambda expression that squares the input double value. The code for this approach is as per following ?

Example

import java.util.function.DoubleUnaryOperator;

public class Approach1 {
   public static void main(String[] args) {
      DoubleUnaryOperator square = (double x) -> x * x;
      double result = square.applyAsDouble(3.0);
      System.out.println(result); 
   }
}

Output

9.0

Explanation of the Code

We characterize a lambda expression utilizing the DoubleUnaryOperator interface, that takes a double value as the input and returns its square value.

The lambda articulation (double x) - > x * x increases the input value x with itself, successfully squaring out it.

We make an instance of the DoubleUnaryOperator utilizing the lambda articulation and allot it to the variable square.

We apply the DoubleUnaryOperator to the worth 3.0 utilizing the applyAsDouble strategy, coming about the square of 3.0, which is 9.0.

At last, we print the outcome to the console.

Approach 2: Method reference for computing the square root of a double value

In this methodology, we will carry out a custom technique that computes the square root of the input double value. The code for this approach is as per the following ?

Example

import java.util.function.DoubleUnaryOperator;

public class Approach2 {
   public static void main(String[] args) {
      DoubleUnaryOperator squareRoot = Math::sqrt;
      double result = squareRoot.applyAsDouble(16.0);
      System.out.println(result); 
   }
}

Output

4.0

Explanation of the Code

We create a DoubleUnaryOperator instance named squareRoot using a method reference to the sqrt method from the Math class.

The sqrt strategy works out the square base of a given double value.

We apply the DoubleUnaryOperator to the value 16.0 utilizing the applyAsDouble strategy, bringing about the square base of 16.0, which is 4.0.

At last, we print the outcome to the console.

Approach 3: Lambda expression for converting degrees to radians

In this methodology, we will utilize a lambda expression to switch the information double value from degrees over completely to radians. The code for this approach is as per the following ?

Example

import java.util.function.DoubleUnaryOperator;

public class Approach3 {
   public static void main(String[] args) {
      DoubleUnaryOperator degreesToRadians = (double degrees) -> Math.toRadians(degrees);
      double result = degreesToRadians.applyAsDouble(90.0);
      System.out.println(result); 
   }
}

Output

1.5707963267948966 

Explanation of the Code

We characterize a lambda expression utilizing the DoubleUnaryOperator interface, which switches a degree value over completely to its equivalent in radians utilizing the toRadians strategy from the Math class.

The lambda expression (double degrees) -> Math.toRadians(degrees) converts the input value degrees to radians.

We create an instance of the DoubleUnaryOperator using the lambda expression and assign it to the variable degreesToRadians.

We apply the DoubleUnaryOperator to the value 90.0 using the applyAsDouble method, resulting in the conversion of 90.0 degrees to radians, which is approximately 1.5707963267948966.

At last, we print the outcome to the console..

Approach 4: Lambda expression for computing the exponential value of a double.

In this methodology, we will make a lambda expresion to compute the exponential value of the input double value. The code for this approach is as per the following ?

Example

import java.util.function.DoubleUnaryOperator;

public class Approach4 {
   public static void main(String[] args) {
      DoubleUnaryOperator exponential = (double x) -> Math.exp(x);
      double result = exponential.applyAsDouble(2.0);
      System.out.println(result); 
   }
}

Output

7.38905609893065

Explanation of the Code

We characterize a lambda expression utilizing the DoubleUnaryOperator interface, which computes the exponential value of the input utilizing the exp technique from the Math class.

The lambda expression (double x) -> Math.exp(x) applies the exp method to the input value x, resulting in the exponential value.

We create an instance of the DoubleUnaryOperator using the lambda expression and assign it to the exponential variable.

We apply the DoubleUnaryOperator to the value 2.0 using the applyAsDouble method, resulting in the exponential value of 2.0, which is approximately 7.38905609893065.

Finally, we print the result to the console.

Conclusion

The DoubleUnaryOperator interface in Java gives a helpful method for characterizing procedure on a single double value. It permits us to make lambda expressions or technique references that epitomize these activities. By utilizing the DoubleUnaryOperator interface, we can compose compact and expressive code that performs numerical estimations, transformations, or some other procedure on double values. Integrating this point of interaction into your Java projects can upgrade their readability and maintainability. Thus, next time you want to characterize an unary procedure on double values, think about utilizing the force of the DoubleUnaryOperator interface in Java.

Updated on: 2023-07-31T17:34:59+05:30

148 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements