DoubleConsumer Interface in Java with ExamplesThe java.util.function package, which was first released with Java 8, includes the DoubleConsumer Interface, which is used to do functional programming in Java. It is an example of a function that accepts a single double-valued argument but outputs nothing. In order to define its accept() function, which ultimately applies the specified operation on its single parameter, a lambda expression of the DoubleConsumer type is assigned to the object. Using an object of type Consumer<Double> is comparable. The two functions that comprise the DoubleConsumer interface are given by
1. accept()This method operates on the one parameter it receives and takes one value. Syntax:Parameters: This method only accepts value as the input argument, which is the only parameter. Returns: There is no value returned by this method. Example 1:The program that follows shows how to handle a double value by using a defined operation using the DoubleConsumer functional interface. Here, a DoubleConsumer instance called display is defined using a lambda expression. It takes a double input, multiplies it by 100, and prints the result along with an explanation. Implementation: FileName: Doubleconsumer.java Output: The value is given by 400.0 Example 2:The given Java program shows how to add double values to a list using the DoubleConsumer functional interface. In order to store Double data, a list called List must first be created. Implementation: FileName: AddingvaluestoList.java Output: The elements present in the given list is: [1.0, 2.0, 3.0, 4.0, 5.0] 2. andThen()The parameterized DoubleConsumer continues to execute after the first one, and it returns a composed DoubleConsumer. An error is reported to the caller of the composite operation if the evaluation of any operation returns an error. Note: It should be noticed that the operation being supplied as an argument is of the DoubleConsumer type.Syntax:Parameters: The DoubleConsumer that will be given after the current one is the parameter that this function accepts. Return Value: The result of this function is a created DoubleConsumer that performs the after operation after applying the current operation. Exception: If the after operation is null, then this method raises a NullPointerException. Example 1:The DoubleConsumer interface and has andThen method for chaining operations are used in the Java program as an example. Implementation: FileName: DoubleconsumerthenMethod.java Output: The value is given by : 40.0 Example 2:The DoubleConsumer interface and its andThen method are used in the Java program to demonstrate a unique instance. Implementation: FileName: ExceptionalCaseDoubleConsumer.java Output: The Exception thrown is : java.lang.NullPointerException Example 3:The Java program shows how to use the DoubleConsumer interface and function to manage exceptions. Implementation: FileName: HandlingExceptionDoubleConsumer.java Output: The Exception thrown is : java.lang.NumberFormatException: For input string: "4.0" |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India