DoubleConsumer Interface in Java with Examples


In Java, functional programming has acquired enormous fame because of its capacity to work on code and further develop code coherence. The DoubleConsumer interface point is a significant part of Java's functional programming worldview.It is a functional interface that accepts a double-valued argument and performs some operations on it. In this article, we will explore the syntax, usage, and examples of the DoubleConsumer interface in Java.

Syntax

The DoubleConsumer interface has a solitary conceptual strategy called accept, which takes a double value as its parameter. The syntax of the DoubleConsumer point of interface is as per the following:

@FunctionalInterface
public interface DoubleConsumer {
   void accept(double value);
}

Explanation of Syntax

  • The '@FunctionalInterface' comment demonstrates that this interface is a functional interface and can be utilized as the assignment target for a lambda expression or a strategy reference.

  • The DoubleConsumer interface is declared with the ‘public’ access modifier.

  • The 'DoubleConsumer' interface characterizes a solitary strategy called 'accept', which accepts a double value as its input parameter.

  • It is the main method used to perform operations on the input double value.

  • The `accept` method does not return any value (void).

  • It is meant to perform some operations on the input double value rather than returning a result.

Example

Here's an example of how the DoubleConsumer interface can be used in code.

import java.util.function.DoubleConsumer;

public class DoubleConsumerExample {
   public static void main(String[] args) {
      DoubleConsumer printSquare = (num) -> System.out.println(num * num);
      printSquare.accept(5.5);
   }
}

Output

30.25

Explanation

  • In the above code, we will import the 'DoubleConsumer' interface from the 'java.util.function' package.

  • We make an instance of the 'DoubleConsumer' interface utilizing a lambda expression '(num) - > System.out.println(num * num)'.

  • The lambda expression works out the square of the input number and prints it.

  • At last, we call the 'accept' technique on the 'printSquare' instance and pass a double value '5.5' as a argument.

Step-by-Step Algorithm

To better understand the utilization of the DoubleConsumer interface, we should go through a step-by-step algorithm:

  • Import the `java.util.function.DoubleConsumer` package.

  • Create an instance of the `DoubleConsumer` interface using a lambda expression or a method reference.

  • Implement the desired functionality inside the `accept` method.

  • Call the `accept` method on the created instance and pass the required double value as an argument.

Approach 1:Square root calculation using DoubleConsumer interface.

In this approach, we will use the DoubleConsumer interface to print the square root of a given double value.

Example

import java.util.function.DoubleConsumer;

public class SquareRootExample {
   public static void main(String[] args) {
      DoubleConsumer printSquareRoot = (num) -> System.out.println(Math.sqrt(num));
      printSquareRoot.accept(16.0);
   }
}

Output

4.0

Explanation of the Code

We make an instance of the 'DoubleConsumer' interface utilizing a lambda expression '(num) - > System.out.println(Math.sqrt(num))'.

The lambda expression works out the square root of the input number utilizing the 'Math.sqrt()' strategy and prints it.

At last, we call the 'accept' strategy on the 'printSquareRoot' instance and pass a tdouble value '16.0' as a argument.

This approach showcases the flexibility of the DoubleConsumer interface in performing mathematical operations on double values.

Approach 2: Exponential value calculation using DoubleConsumer interface.

In this approach, we will use the DoubleConsumer interface to calculate the exponential value of a given double value.

Example

import java.util.function.DoubleConsumer;

public class ExponentialValueExample {
   public static void main(String[] args) {
      DoubleConsumer printExponentialValue = (num) -> System.out.println(Math.exp(num));
      printExponentialValue.accept(2.0);
   }
}

Output

7.38905609893065

Explanation of the Code

We make an instance of the 'DoubleConsumer' interface utilizing a lambda expression '(num) - > System.out.println(Math.exp(num))'.

The lambda expression works out the exponential value of the input number utilizing the 'Math.exp()' strategy and prints it.

At last, we call the 'accept' strategy on the 'printExponentialValue' instance and pass a double value `2.0` as an argument.

This approach demonstrates how the DoubleConsumer interface can be utilized to perform complex mathematical computations on double values.

Approach 3: Rounding off to the nearest integer using DoubleConsumer interface

In this approach, we will use the DoubleConsumer interface to round off a given double value to the nearest integer.

Example

import java.util.function.DoubleConsumer;

public class RoundOffExample {
   public static void main(String[] args) {
      DoubleConsumer printRoundedValue = (num) -> System.out.println(Math.round(num));
      printRoundedValue.accept(3.7);
   }
}

Output

4

Explanation of the Code

We make an instance of the 'DoubleConsumer' interface utilizing a lambda articulation '(num) - > System.out.println(Math.round(num))'.

The lambda expression adjusts the input number to the closest integer utilizing the 'Math.round()' technique and prints it.

At last, we call the 'accept' strategy on the 'printRoundedValue' instance and pass a double value `3.7` as an argument.

This approach illustrates how the DoubleConsumer interface can be used to achieve rounding off functionality for double values.

Approach 4: Custom operation on a double value using DoubleConsumer interface

In this approach, we will use the DoubleConsumer interface to perform a custom operation on a given double value.

Example

import java.util.function.DoubleConsumer;

public class CustomOperationExample {
   public static void main(String[] args) {
      DoubleConsumer customOperation = (num) -> {
         // Perform custom operation on num
         double result = num * 2 + 10;
         System.out.println("Result: " + result);
      };
      customOperation.accept(5.0);
   }
}

Output

Result: 20.0

Explanation of the Code

We make an instance of the 'DoubleConsumer' interface utilizing a lambda expression.

Inside the lambda articulation, we characterize a custom operation on the input number 'num' and store the outcome in the 'result' variable.

At long last, we print the outcome utilizing the 'System.out.println()' strategy.

We call the 'accept' technique on the 'customOperation' instance and pass a double value `5.0` as an argument.

This approach highlights the versatility of the DoubleConsumer interface, allowing developers to define and execute their custom operations on double values.

Conclusion

In this article, we explored the DoubleConsumer interface in Java. We learned about its syntax, usage, and various examples. The DoubleConsumer interface is a powerful tool for functional programming in Java, allowing us to write concise and readable code by encapsulating operations on double values. By leveraging the DoubleConsumer interface, developers can enhance their code with the benefits of functional programming, improving maintainability and code quality.

Updated on: 31-Jul-2023

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements