Java 8 Consumer Interface with forEach Loop

In this tutorial we will learn how to use the Java 8 Consumer Interface and the forEach loop for iterating the list of Customer objects.

Java 8 Consumer Interface with forEach Loop

In this tutorial we will learn how to use the Java 8 Consumer Interface and the forEach loop for iterating the list of Customer objects.

Java 8 Consumer Interface with forEach Loop

How to use the Java 8 Consumer Interface with forEach Loop?

In this tutorial I will explain you about the Java 8 Consumer Interface and then explain you how to use the forEach loop in Java to iterate through the collection of classes. We will show you how to perform some operation in the forEach loop?

In this example we will create a class "Customer.java" and the show you how you can use the Java 8 Consumer Interface and then iterate through the class with the help of forEach loop.

In this example we have used the Consumer functional interface and the forEach loop which is added as a new features in the Java 8. Here we have used the Consumer interface as as assignment to the lambda expression and with the help of forEach loop the values in the collection is iterated and performed some operation (printValue()) on the object of Cusomer.java class.

We have also explained you how to iterate the collection in different ways.

Here is the code of the Customer class (Customer.java):

/*
* @author: Deepak Kumar
* Web: http://www.roseindia.net
*/
public class Customer {
    public String name;
    public String address;
    public Customer(String name,String address){
        this.name = name;
        this.address = address;
    }
    public void printValue(){
	//Print the values
        System.out.println("Name:"+name + " Address:"+address);
    }
}

In the above class we have a method printValue(), we show you how you can iterate through the list of Customer objects and then call the printValue() method.

Advertisement

Now I will show you the example of creating the array of the Customer class and then different ways of printing the data by iterating the list. Here is the complete code of the program:

/*
* @author: Deepak Kumar
* Web: http://www.roseindia.net
*/
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerInterfaceForEachLoopExample {
   public static void main(String[] args) {
        
	List list = new ArrayList();

        list.add(new Customer("Deepak","Delhi"));
        list.add(new Customer("Gaurav","Mumbai"));
        list.add(new Customer("John","UK"));
	list.add(new Customer("Abby","UK"));
	list.add(new Customer("Alice","UK"));
		
        //Intance of Consumer functional interface
        Consumer style = (Customer c) -> System.out.println("Name:"+c.name +" and Address:"+c.address);
		
	System.out.println("\nPringing using method 1");
	//Way 1 for iterating and printing values
        list.forEach(style);
		
	System.out.println("\nPringing using method 2");
        //Way 2 for iterating using the method reference 
        list.forEach(Customer::printValue);

	System.out.println("\nPringing using method 3");
        //Way 3 using the lambda Expression
        list.forEach((Customer c) -> c.printValue());
   }
} 

In the above example code you can see the three ways of iterating and printing the data.

Here are the three ways:

//Way 1 for iterating and printing values
        list.forEach(style);

//Way 2 for iterating using the method reference
        list.forEach(Customer::printValue);

//Way 3 using the lambda Expression
        list.forEach((Customer c) -> c.printValue());

So, in this tutorial you have learned the ways to iterate over the collection in the Java 8.

Read more tutorials of Java 8 at our Java 8 tutorials index page.