A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/iterable-foreach-method-in-java-with-examples/ below:

Iterable forEach() Method in Java

Iterable forEach() Method in Java

Last Updated : 11 Jul, 2025

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)

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4