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