Consumer interface in Java

The consumer interface of the functional interface accepts only one single argument without any return value. The consumer interface has no return value. It contains an abstract accept() and a default andThen() method. It can be used as the assignment target for a lambda expression or method reference.

  1. The consumer interface of the functional interface accepts only one single argument without any return value.
  2. The consumer interface has no return value.
  3. It contains an abstract accept() and a default andThen() method.
Methods of Consumer Interface.
  1. void accept(T t)
  2. The accept() method takes as input the type T and returns no value. This is the single abstract method of Consumer. It accepts a single generic argument of type T and returns nothing (i.e. void). This is the method that's implemented by a lambda expression or method reference. details..
  3. andThen(Consumer after)
  4. The andThen() is a default method in the interface. In other words, it has an implementation and it is non-abstract method. The method accepts a Consumer and returns another Consumer. details..
    The andThen() method allows multiple Consumer operations sequentialy.

Example of accept() method of Consumer Interface

import java.util.function.*;
public class ConsumerInterfaceAcceptExample{

    static void  welcomeGreeting ( String name){
        System.out.println( "Welcome "+name );
    }
    static void showAge (int age){
        System.out.println( "Age is "+age );
    }
    static void showSalary (int sal){
        System.out.println( "Salary is "+sal );
    }

    public static void main(String[] args) {
        Consumer<String> consumer1 =
            ConsumerInterfaceExample :: welcomeGreeting;
        // Calling Consumer method
        consumer1.accept( "Tejas" ); 

        //method reference    
        Consumer<Integer> consumer2 = 
        ConsumerInterfaceExample :: showAge; 
        // Calling Consumer method
        consumer2.accept( 30 );

        //method reference
        Consumer<Integer> consumer2 = 
        ConsumerInterfaceExample :: showSalary; 
        // Calling Consumer method 
        consumer2.accept( 20000 );
    }
}

Output:
    Welcome Tejas
    Age is 30
    Salary is 20000

Example of andThen() method of Consumer Interface

  1. The andThen() method allows multiple Consumer operations sequentialy.
  2. If the original Consumer (the one calling andThen()) throws an exception, the after Consumer will not be executed.
import java.util.function.Consumer;
public class ConsumerInterfaceAndThenExample{

  public static void main(String[] args) {
        //method reference 
        Consumer<String> consumer =
            s -> System.out.println( "Welcome "+s );

        Consumer<String> consumer2 =
            s -> System.out.println( "Welcome "+s+ "to the World!" );
        
        Consumer<String> consumer2 =
            consumer.andThen( consumer2 );

         // Calling Consumer method
         consumer.accept( "Rahul" ); 
    }
}

Output:
    Welcome Rahul
    Welcome Rahul to the World!

Bi-Consumer

The different between Consumer and Bi-Consumer is Consumer takes only one argument, but Bi-Consumer interface takes two arguments. Both, Consumer and Bi-Consumer have no return value. It is used in iterating through the entries of the map.

BiConsumer<String,String> foo = (a, b) -> System.out.println( a + b );