The three forms of looping are nearly identical. The enhanced for loop:

for (E element : list) {
    . . .
}

is, according to the Java Language Specification, identical in effect to the explicit use of an iterator with a traditional for loop. In the third case, you can only modify the list contents by removing the current element and, then, only if you do it through the remove method of the iterator itself. With index-based iteration, you are free to modify the list in any way. However, adding or removing elements that come before the current index risks having your loop skipping elements or processing the same element multiple times; you need to adjust the loop index properly when you make such changes.

In all cases, element is a reference to the actual list element. None of the iteration methods makes a copy of anything in the list. Changes to the internal state of element will always be seen in the internal state of the corresponding element on the list.

Essentially, there are only two ways to iterate over a list: by using an index or by using an iterator. The enhanced for loop is just a syntactic shortcut introduced in Java 5 to avoid the tedium of explicitly defining an iterator. For both styles, you can come up with essentially trivial variations using for, while or do while blocks, but they all boil down to the same thing (or, rather, two things).

EDIT: As @iX3 points out in a comment, you can use a ListIterator to set the current element of a list as you are iterating. You would need to use List#listIterator() instead of List#iterator() to initialize the loop variable (which, obviously, would have to be declared a ListIterator rather than an Iterator).

Answer from Ted Hopp on Stack Overflow
🌐
Java Guides
javaguides.net › 2019 › 11 › java-8-foreach-with-list-set-and-map.html
Java 8 forEach with List, Set and Map Examples
September 14, 2020 - Let's use normal for-loop to loop ... { System.out.println(item.getName()); } } In Java 8, you can loop a List with forEach + lambda expression or method reference....
🌐
Baeldung
baeldung.com › home › java › java list › ways to iterate over a list in java
Ways to Iterate Over a List in Java | Baeldung
June 27, 2025 - As we can see, the ListIterator ... System.out.println(listIterator.next()); } Since Java 8, we can use the forEach() method to iterate over the elements of a list....
Top answer
1 of 13
337

The three forms of looping are nearly identical. The enhanced for loop:

for (E element : list) {
    . . .
}

is, according to the Java Language Specification, identical in effect to the explicit use of an iterator with a traditional for loop. In the third case, you can only modify the list contents by removing the current element and, then, only if you do it through the remove method of the iterator itself. With index-based iteration, you are free to modify the list in any way. However, adding or removing elements that come before the current index risks having your loop skipping elements or processing the same element multiple times; you need to adjust the loop index properly when you make such changes.

In all cases, element is a reference to the actual list element. None of the iteration methods makes a copy of anything in the list. Changes to the internal state of element will always be seen in the internal state of the corresponding element on the list.

Essentially, there are only two ways to iterate over a list: by using an index or by using an iterator. The enhanced for loop is just a syntactic shortcut introduced in Java 5 to avoid the tedium of explicitly defining an iterator. For both styles, you can come up with essentially trivial variations using for, while or do while blocks, but they all boil down to the same thing (or, rather, two things).

EDIT: As @iX3 points out in a comment, you can use a ListIterator to set the current element of a list as you are iterating. You would need to use List#listIterator() instead of List#iterator() to initialize the loop variable (which, obviously, would have to be declared a ListIterator rather than an Iterator).

2 of 13
55

Example of each kind listed in the question:

ListIterationExample.java

import java.util.*;

public class ListIterationExample {

     public static void main(String []args){
        List<Integer> numbers = new ArrayList<Integer>();

        // populates list with initial values
        for (Integer i : Arrays.asList(0,1,2,3,4,5,6,7))
            numbers.add(i);
        printList(numbers);         // 0,1,2,3,4,5,6,7

        // replaces each element with twice its value
        for (int index=0; index < numbers.size(); index++) {
            numbers.set(index, numbers.get(index)*2); 
        }
        printList(numbers);         // 0,2,4,6,8,10,12,14

        // does nothing because list is not being changed
        for (Integer number : numbers) {
            number++; // number = new Integer(number+1);
        }
        printList(numbers);         // 0,2,4,6,8,10,12,14  

        // same as above -- just different syntax
        for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext(); ) {
            Integer number = iter.next();
            number++;
        }
        printList(numbers);         // 0,2,4,6,8,10,12,14

        // ListIterator<?> provides an "add" method to insert elements
        // between the current element and the cursor
        for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext(); ) {
            Integer number = iter.next();
            iter.add(number+1);     // insert a number right before this
        }
        printList(numbers);         // 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

        // Iterator<?> provides a "remove" method to delete elements
        // between the current element and the cursor
        for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext(); ) {
            Integer number = iter.next();
            if (number % 2 == 0)    // if number is even 
                iter.remove();      // remove it from the collection
        }
        printList(numbers);         // 1,3,5,7,9,11,13,15

        // ListIterator<?> provides a "set" method to replace elements
        for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext(); ) {
            Integer number = iter.next();
            iter.set(number/2);     // divide each element by 2
        }
        printList(numbers);         // 0,1,2,3,4,5,6,7
     }

     public static void printList(List<Integer> numbers) {
        StringBuilder sb = new StringBuilder();
        for (Integer number : numbers) {
            sb.append(number);
            sb.append(",");
        }
        sb.deleteCharAt(sb.length()-1); // remove trailing comma
        System.out.println(sb.toString());
     }
}
🌐
GeeksforGeeks
geeksforgeeks.org › java › iterate-through-list-in-java
Iterate through List in Java - GeeksforGeeks
July 23, 2025 - This feature is available since Java 8. It can also be used to iterate over a List. Iteration can be done using a lambda expression. Syntax: list_name.forEach(variable->{//block of code}) Below is the example of this method: Java ·
🌐
Baeldung
baeldung.com › home › java › core java › guide to the java foreach loop
Guide to the Java forEach Loop | Baeldung
June 17, 2025 - This interface has a new API starting ... the action throws an exception.” · And so, with forEach(), we can iterate over a collection and perform a given action on each element....
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
For Each Loop Java 8 Example - Java Code Geeks
April 29, 2021 - From Java 8 and on, developers ... of this enhanced for loop. The new Stream class provides a forEach method, which can be used to loop over all of the selected elements of List, Set, and Map....
🌐
Mkyong
mkyong.com › home › java8 › java 8 foreach examples
Java 8 forEach examples | mkyong.com
December 4, 2020 - public static void loopListJava8() { List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add(null); list.add("D"); list.add("E"); // filter null value list.stream() .filter(Objects::nonNull) .forEach(System.out::println); } ... P.S The forEach for Set and Stream works the same way. 3.1 Review the forEach method signature, it accepts a functional interface Consumer. ... public interface Iterable<T> { default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } //.. }
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java foreach()
Java forEach() with Examples - HowToDoInJava
February 6, 2023 - List<String> list = Arrays.asList("Alex", "Brian", "Charles"); list.forEach(System.out::println); Since Java 8, the forEach() has been added in the following classes or interfaces:
Find elsewhere
🌐
Blogger
javarevisited.blogspot.com › 2015 › 09 › java-8-foreach-loop-example.html
Java 8 forEach() Loop Example
Java 8 has introduced a new way ... over any Collection like List, Set, or Map by converting them into a java.util.sttream.Stream instance and then calling the forEach() method....
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-foreach-method-in-java
ArrayList forEach() Method in Java - GeeksforGeeks
July 20, 2026 - Simplifies iteration without using explicit loops. Introduced in Java 8 as part of the Collections Framework enhancements. ... import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Create an ArrayList of Strings ArrayList<String> s = new ArrayList<>(); s.add("Cherry"); s.add("Blueberry"); s.add("Strawberry"); // Use forEach() to print each fruit s.forEach(System.out::println); } }
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-8-foreach
Java 8 forEach method with example
Inside forEach we are using a lambda expression to print each element of the list. import java.util.List; import java.util.ArrayList; public class Example { public static void main(String[] args) { List<String> fruits = new ArrayList<String>(); fruits.add("Apple"); fruits.add("Orange"); ...
🌐
Spring Framework Guru
springframework.guru › home › java 8 foreach
Java 8 forEach - Spring Framework Guru
May 20, 2019 - With both the new forEach method and the Java 8 Stream API, you can create a stream of elements in a collection and then pipeline the stream to a forEach method for iteration. The code to iterate through a stream of elements in a List is this.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java 8 tutorial › java 8 foreach
Java 8 forEach | Default Method Defined in the Iterable Interface.
February 14, 2023 - The foreach method in Java 8 is used to iterate over the elements. This method is defined in the stream and iterable interfaces. This is the default method defined in the iterable interface.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
ZetCode
zetcode.com › java › foreach
Java forEach - forEach on Java lists, maps, sets
June 17, 2025 - The forEach method executes a specified action for each element within an Iterable object, continuing until all elements are processed or an exception occurs. Introduced in Java 8, this method offers developers a modern, concise alternative to traditional looping constructs, simplifying the ...
🌐
Codemia
codemia.io › home › knowledge hub › ways to iterate over a list in java
Ways to iterate over a list in Java | Codemia
January 25, 2025 - The ListIterator extends Iterator ... (listIterator.nextIndex() == 2) { 6 listIterator.add("Date"); 7 } 8} Introduced in Java 8, the forEach() method combined with lambda expressions provides a very readable and concise way to iterate over a list....
🌐
Java67
java67.com › 2016 › 01 › how-to-use-foreach-method-in-java-8-examples.html
10 Examples of forEach() method in Java 8 | Java67
Now you know a little bit about ... method as shown below: List<String> alphabets = new ArrayList<>(Arrays.asList("aa", "bbb", "cat", "dog")); alphabets.forEach(s -> System.out.println(s));...
🌐
Delft Stack
delftstack.com › home › howto › java › iterate list java
How to Iterate Through List in Java | Delft Stack
February 2, 2024 - import java.util.ArrayList; import ...tem.out::println); } } ... If you are working with stream API, use the forEach() of stream() to print out all the list elements....
🌐
Medium
medium.com › javarevisited › i-need-an-index-with-this-list-iteration-method-1e339fd55ed7
I need an index with this List iteration method | by Donald Raab | Javarevisited | Medium
February 10, 2024 - Since Java 8, it is possible to use IntStream.range() to iterate over a set of indices and use List look ups using get(). IntStream.range() can be used an object-oriented version of an indexed for-loop.
🌐
TutorialsPoint
tutorialspoint.com › how-to-iterate-a-list-using-for-each-loop-in-java
How to iterate a List using for Loop in Java?
May 26, 2025 - import java.util.List; import java.util.Stack; public class iterateOverList { public static void main(String[] args) { //instantiating a List using Stack class List<String> vowels = new Stack<>(); //adding element to it vowels.add("A"); vowels.add("E"); vowels.add("I"); vowels.add("O"); vowels.add("U"); System.out.println("The list elements are: "); //iterate over a vowels using for-loop for(int i= 0; i<vowels.size(); i++) { System.out.print(vowels.get(i) + " "); } } } Following is the output of the above program: The list elements are: A E I O U · Vivek Verma · Updated on: 2025-05-26T19:48:39+05:30 · 9K+ Views · How to use for and foreach loops to display elements of an array using Java Javaexamples ·
🌐
Medium
neesri.medium.com › master-in-java-8-foreach-48ac3fc940dc
Master in the forEach() Method in Java 8 | by A cup of JAVA coffee with NeeSri | Medium
August 3, 2024 - List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); // Using forEach to double each number and print the result numbers.forEach(num -> System.out.println(num * 2)); ... Using forEach is a concise and expressive way to work with collections ...