DoubleConsumer Interface in Java with Examples
Last Updated :
04 Aug, 2022
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:
accept()
This method accepts one value and performs the operation on its only argument. Syntax:
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:
Java
import java.util.function.DoubleConsumer;
public class GFG {
public static void main(String args[])
{
// Create a DoubleConsumer Instance
DoubleConsumer
display
= a -> System.out.println(a * 10);
// using accept() method
display.accept(3);
}
}
andThen()
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 . Syntax:
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 1:
Java
import java.util.function.DoubleConsumer;
public class GFG {
public static void main(String args[])
{
// Create a DoubleConsumer Instance
DoubleConsumer display = a -> System.out.println(a * 10);
DoubleConsumer mul = a -> a /= 2;
// using addThen() method
DoubleConsumer composite = mul.andThen(display);
composite.accept(3);
}
}
Program 2: To demonstrate when NullPointerException is returned.
Java
import java.util.function.DoubleConsumer;
public class GFG {
public static void main(String args[])
{
// Create a DoubleConsumer Instance
DoubleConsumer mul = a -> a /= 10;
try {
// using addThen() method
DoubleConsumer composite = mul.andThen(null);
composite.accept(3);
}
catch (Exception e) {
System.out.println("Exception : " + e);
}
}
}
Output:Exception : java.lang.NullPointerException
Program 3: To demonstrate how an Exception in the after function is returned and handled.
Java
import java.util.function.DoubleConsumer;
public class GFG {
public static void main(String args[])
{
try {
DoubleConsumer
conv
= a
-> System.out.println(
Integer
.parseInt(
Double
.toString(a)));
DoubleConsumer mul = a -> a /= 10;
// using addThen() method
DoubleConsumer composite = mul.andThen(conv);
composite.accept(3);
}
catch (Exception e) {
System.out.println("Exception : " + e);
}
}
}
Output:Exception : java.lang.NumberFormatException: For input string: "3.0"
Similar Reads
Java 8 | Consumer Interface in Java with Examples The Consumer 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 argument and produces a result. However these kind of functions don't return any value.Hence this functi
4 min read
Java 8 | BiConsumer Interface in Java with Examples The BiConsumer 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 that takes in two arguments and produces a result. However, these kinds of functions doesn't return any value. This funct
5 min read
Inheritance of Interface in Java with Examples Inheritance is an important pillar of OOPs(Object Oriented Programming). It is the mechanism in java by which one class is allowed to inherit the features(fields and methods) of another class. Like a class , an interface can have methods and variables, but the methods declared in an interface are by
9 min read
Java Program that Shows Use of Collection Interface The Collection framework is a unified architecture for representing and manipulating collections, enabling collections to be manipulated independently of implementation details. Uses and advantages of Collection Framework: This reduces the efforts of programmers by providing data structures and algo
3 min read
Extends vs Implements in Java In Java, the extends keyword is used to inherit all the properties and methods of the parent class while the implements keyword is used to implement the method defined in an interface.Difference Between extends and implements KeywordS.No.ExtendsImplements1.By using "extends" keyword a class can inhe
3 min read
Initializing a List in Java The Java.util.List is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by the ArrayList, LinkedList, Vector, and
6 min read