Consumer Interface in Java with examples

 236 total views,  3 views today

In this tutorial let us learn about Consumer Interface in Java with examples.

Consumer Interface is part of Java 8 and it is available in the java.util.function package.

Consumer Interface is a functional interface.

It will have only one abstract method and a default method.

Interface Description

@FunctionalInterface
public interface Consumer<T> {

   //It is an abstract method
    void accept(T t); 

    //It is a default method
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

Let us take a close look on accept() method

void accept(T t);

If you notice this method,you can easily identify two properties.

  1. It accepts only one argument and it can be of any type E.g., String, Integer, etc
  2. Its return type is void.

This accept method will be executed when the corresponding consumer interface is assigned and invoked. Hence, Consumer interface should be implemented by assigning a Lambda express or method reference or a separate implementation.

Let us see a few examples of consumer interface.

1. Java consumer interface example using lambda expression.

Create a lambda express to print the data and assign it to the consumer interface.

 public static void main(String[] args {
        List<String> list  = new ArrayList<>();
        list.add("Apple");
        list.add("Orange");
        list.add("Banana");
        list.add("Grapes");
        
      //Assigning a lambda expression to consumer interface
       Consumer<String> consumer = (item) -> System.out.println(item);
       
       //pass the consumer object to foreach. 
        list.forEach(consumer);
}

Output:

Apple
Orange
Banana
Grapes
	
  • Lambda expression is assigned to the conumer interface.
  • Consumer object passed to foreach method.

2. Java consumer interface example using Method reference.

Here I have created a separate static method to print the data. I have assigned the method to the consumer interface by making static method reference.

public class Test
{

    public static void main(String[] args)
    {
        List<String> list  = new ArrayList<>();
        list.add("Apple");
        list.add("Orange");
        list.add("Banana");
        list.add("Grapes");
        
       //Method reference
       Consumer<String> consumerObj = Test::print;
       
       //Pass the consumerObj and print the elements
       list.forEach(consumerObj);
       
	}
	
	static void print(String item) {
        System.out.println(item);
    }
}

Output:
Apple
Orange
Banana
Grapes

3. Java consumer interface example using implementation

This is a classic way of implementing an interface.

public class Test
{

    public static void main(String[] args)
    {
        List<String> list  = new ArrayList<>();
        list.add("Apple");
        list.add("Orange");
        list.add("Banana");
        list.add("Grapes");
		
		//Using Consumer
        Consumer<String> consumer = new Consumer<String>()
        {
            
            @Override
            public void accept(String obj)
            {
                System.out.println(obj);
                
            }
        };
        list.forEach(consumer);
		
	}
}

Output:
Apple
Orange
Banana
Grapes

Reference : Official documentation.

Thanks for reading Consumer interface in Java.Please provide your valuable comments.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.