The DoubleConsumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one double-valued argument but does not return any value.
The lambda expression assigned to an object of DoubleConsumer type is used to define its accept() which eventually applies the given operation on its only argument. It is similar to using an object of type Consumer<Double>
The DoubleConsumer interface consists of the following two functions:
This method accepts one value and performs the operation on its only argument.
void accept(double value)
Parameters: This method takes in only one parameter:
- value– the input argument
Returns: This method does not return any value.
Below is the code to illustrate accept() method:
It returns a composed DoubleConsumer wherein the parameterized DoubleConsumer will be executed after the first one. If the evaluation of either operation throws an error, it is relayed to the caller of the composed operation.
Note: The operation being passed as the argument should be of type DoubleConsumer .
default DoubleConsumer andThen(DoubleConsumer after)
Parameters: This method accepts a parameter after which is the DoubleConsumer to be applied after the current one.
Return Value: This method returns a composed DoubleConsumer that first applies the current operation first and then the after operation.
Exception: This method throws NullPointerException if the after operation is null.
Below is the code to illustrate andThen() method:
Program 2: To demonstrate when NullPointerException is returned.
Exception : java.lang.NullPointerException
Program 3: To demonstrate how an Exception in the after function is returned and handled.
Exception : java.lang.NumberFormatException: For input string: "3.0"
- OptionalDouble ifPresent(DoubleConsumer) method in Java with examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Comparator Interface in Java with Examples
- SortedSet Interface in Java with Examples
- SortedMap Interface in Java with Examples
- LongToDoubleFunction Interface in Java with Examples
- Function Interface in Java with Examples
- LongConsumer Interface in Java with Examples
- List Interface in Java with Examples
- Java 8 | DoubleSupplier Interface with Examples
- Java 8 | LongSupplier Interface with Examples
- Java 8 | IntSupplier Interface with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.