Consumer Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expect an input but produces no output. In this post, we are going to see several implementations of Consumer Interface by using different examples.

Consumer_Interface_Java8_Techndeck

 

Consumer Interface in Java 8 with Examples...!!! Share on X

Look at Consumer Javadoc description below:

 

 

 

 

Consumer Interface contains 2 methods:

  1. accept 

  2. andThen

 

Let’s discuss these methods:

accept 

This method accepts one input argument and performs operation on the given argument.

Syntax:

void accept(T t);

 

Below are the several examples to demonstrate accept() method:

Example 1. with Integer

import java.util.function.Consumer;

public class ConsumerInterfaceJava8Example1 {

    public static void main(String args[]) {

        System.out.println("Ex. 1 - Java 8 Consumer Interface\n");

        Consumer <Integer> consumerObj = a -> System.out.println(a * 5);

        consumerObj.accept(10);
    }
}

 

Example 2. with HashMap

import java.util.HashMap;
import java.util.function.Consumer;

public class ConsumerInterfaceJava8Example3 {

    public static void main(String args[]) {

        System.out.println("Ex. 2 - Java 8 Consumer Interface\n");

        Consumer <HashMap <Integer, String>> consumerListObj = map -> System.out.println(map);

        HashMap <Integer, String> hashMap = new HashMap <Integer, String> ();

        hashMap.put(1, "Isha");
        hashMap.put(3, "Noni");

        consumerListObj.accept(hashMap);
    }
}

 

Example 3. with ArrayList using Stream API 

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerInterfaceJava8Example2 {

    public static void main(String args[]) {

        System.out.println("Ex. 3 - Java 8 Consumer Interface\n");

        Consumer <List <Integer>> consumerListObj = list -> list.stream().forEach(listValue -> System.out.print(listValue + "\n"));

        List <Integer> sampleList = new ArrayList <Integer> ();

        sampleList.add(3);
        sampleList.add(44);
        sampleList.add(91);
        sampleList.add(75);
        sampleList.add(8);

        consumerListObj.accept(sampleList);
    }
}

 

Example 4. with custom Class object (like ‘Students’ in this case)

import java.util.function.Consumer;

public class ConsumerInterfaceJava8Example4 {

    public static void main(String args[]) {

        System.out.println("Ex. 4 - Java 8 Consumer Interface\n");

        Consumer <Students> consumerListObj = student -> System.out.println("Student details: \n" + "\nName: \n" + student.getName() + "\n\nAge:\n" + student.getAge());

        Students student = new Students(1, "Isha");

        consumerListObj.accept(student);
    }
}

 

Students Class:

public class Students {

    int age;
    String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Students() {}

    public Students(int age, String name) {
        this.age = age;
        this.name = name;
    }

}

 

andThen

Returns a composed Consumer that performs, in sequence, this operation followed by the after operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the after operation will not be performed.

Syntax:

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

 

Below are the several examples to demonstrate andThen() method:

 

Example 5. with String

import java.util.function.Consumer;

public class ConsumerInterfaceJava8Example5 {

    public static void main(String args[]) {
        System.out.println("Ex. 5 - Java 8 Consumer Interface\n");

        Consumer <String> consumerObj = a - > System.out.println(a.length());

        consumerObj.andThen(consumerObj).accept("Techndeck.com");
    }
}

 

Example 6. with ArrayList using Stream API 

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerInterfaceJava8Example6 {

    public static void main(String args[]) {
        System.out.println("Ex. 6 - Java 8 Consumer Interface\n");

        //Add 10 to every integer of the list 
        Consumer <List<Integer>> updatedList = list -> {
            for (int i = 0; i < list.size(); i++)
                list.set(i, 10 + list.get(i));
        };

        //Display a list of integers 
        Consumer <List<Integer>> printList = list -> list.stream().forEach(a -> System.out.print(a + "\n"));

        List <Integer> list = new ArrayList <Integer> ();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

        updatedList.andThen(printList).accept(list);
    }
}

 

Java 8 Consumer Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.

 

Consumer Interface in Java 8 with Examples...!!! Share on X

 

Do you like this Post? – then check my other helpful posts:

Other Useful References:

 

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts