Java 8 DoubleConsumer Interface
7 years ago Lalit Bhagtani 0
In this tutorial, we will learn about Java 8 DoubleConsumer interface.
DoubleConsumer Interface :-
DoubleConsumer ( java.util.function.DoubleConsumer ) is a functional interface that has one abstract method and one default method declared in it. This interface is a specialised form of Consumer interface.
Abstract method :-
void accept ( double value ) :- The abstract method accept ( double value ) is a functional method. It accepts one double value as an argument and return no value, it performs the operation defined by the DoubleConsumer interface ( usually a lambda expression passed as an argument ).
Default method :-
default DoubleConsumer andThen ( DoubleConsumer after ) :- It returns a DoubleConsumer that performs two operation in sequence, first the operation of the DoubleConsumer on which method is called followed by the operation of the DoubleConsumer passed as an argument.
Reference :- DoubleConsumer Interface JavaDocs
Example – 1 :-
This example will show you, how to create and call different methods of a DoubleConsumer interface.
public static void main(String[] args){ DoubleConsumer consumer1 = ( x ) -> { System.out.println(x + " 1st"); }; // Example of accept consumer1.accept(2.00); DoubleConsumer consumer2 = ( x ) -> { System.out.println(x + " 2nd"); }; // Example of andThen consumer1.andThen(consumer2).accept(3.00); }
Result :-
2.0 1st 3.0 1st 3.0 2nd
That’s all for Java 8 DoubleConsumer interface. If you liked it, please share your thoughts in comments section and share it with others too.