Java 8 IntConsumer Interface

9 months ago codedestine 0

In this tutorial, we will learn about Java 8 IntConsumer interface.

Java 8 IntConsumer Interface

IntConsumer Interface :-

IntConsumer ( java.util.function.IntConsumer ) 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 ( int value ) :- The abstract method accept ( int value ) is a functional method. It accepts one int value as an argument and return no value, it performs the operation defined by the IntConsumer interface ( usually a lambda expression passed as an argument ). 

Default method :-  

default  IntConsumer  andThen ( IntConsumer after ) :- It returns a IntConsumer that performs two operation in sequence, first the operation of the IntConsumer on which method is called followed by the operation of the IntConsumer passed as an argument.

Reference :-  IntConsumer Interface JavaDocs 

Example :- 

This example will show you, how to create and call different methods of a IntConsumer interface.

  public static void main(String[] args){ 
 
    IntConsumer consumer1 = ( x ) -> { System.out.println(x + " 1st"); }; 
    // Example of accept 
    consumer1.accept(2);
 
    IntConsumer consumer2 = ( x ) -> { System.out.println(x + " 2nd"); }; 
    // Example of andThen
    consumer1.andThen(consumer2).accept(3); 

  }  

Result :- 

2 1st
3 1st
3 2nd

That’s all for Java 8 IntConsumer Interface. If you liked it, please share your thoughts in comments section and share it with others too.