Open In App

Iterable forEach() Method in Java

Last Updated : 04 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the foreach() method is the default method in the Iterable interface. It provides a simple way to iterate over all elements of an Iterable such as List, Set, etc. using a lambda expression or method reference.

Example 1: This example demonstrates iterating over a List using an Iterator to print each element of the list.

Java
// Java program to demonstrate the working of
// forEach() method of Iterable interface 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
import java.util.function.Consumer; 

public class Geeks { 

	public static void main(String[] args) 
	{ 
		List<String> l = new ArrayList<>(); 
		l.add("New Delhi"); 
		l.add("New York"); 
		l.add("Mumbai"); 
		l.add("London"); 

		Iterator<String> i = l.iterator(); 
		while (i.hasNext()) { 

			System.out.println(i.next()); 		
		} 
	} 
} 

Output
New Delhi
New York
Mumbai
London

Syntax

default void forEach(Consumer<? super T> action)

  • Parameter: The action parameter is a Consumer<? super T> which is a special type of function that takes an item(of type T) and does something with it, but does not return anything. The foreach() method uses this function to apply the action to each item in the Iterable.
  • Return Type: This method does not return any value.
  • Exception: Throws NullPointerException if the input action is null.

Example 2: This example demonstrates using the forEach() method with an anonymous Consumer class to print each element of a List.

Java
// Java program to demonstrate 
// forEach() method with anonymous class
import java.util.ArrayList; 
import java.util.List; 
import java.util.function.Consumer; 

public class Geeks { 

	public static void main(String[] args) 
	{ 
		List<String> data = new ArrayList<>(); 
		data.add("New Delhi"); 
		data.add("New York"); 
		data.add("Mumbai"); 
		data.add("London"); 

		data.forEach(new Consumer<String>() { 

			@Override
			public void accept(String t) 
			{ 

				System.out.println(t); 
			} 

		}); 
	} 
} 

Output
New Delhi
New York
Mumbai
London

Explanation: In the above example, we have created a List of String with 4 elements and then we have iterated over the list using the forEach method. As described earlier forEach method take Consumer object as input, we have created an anonymous inner class implementation of Consumer interface and overrides the accept method. In this example, we have kept the business logic inside the anonymous inner class and we can not reuse it.

Example 3: This example demonstrate the implementation of Consumer interface separately so that we can reuse it. Let’s create a class CityConsumer which implements Consumer interface and overrides its accept method.

Java
// Java program to demonstrate
// uisng a custom Consumer implementation(cityConsumer) with
// the forEach() method to print each element of a List
import java.util.*;
import java.util.function.Consumer;

class CityConsumer implements Consumer<String> {

    @Override public void accept(String t)
    {
        System.out.println(t);
    }
}
// Now we can use the CityConsumer
// with forEach method by just creating
// an object of CityConsumer class as below

public class Geeks {

    public static void main(String[] args)
    {
        List<String> l = new ArrayList<>();
        l.add("New Delhi");
        l.add("New York");
        l.add("Mumbai");
        l.add("London");

        // create an object of CityConsumer
        // and pass it to forEach method
        CityConsumer cityConsumer = new CityConsumer();
        l.forEach(cityConsumer);
    }
}

Output
New Delhi
New York
Mumbai
London


Next Article
Article Tags :
Practice Tags :

Similar Reads