Iterate through List elements

Java Lists can be iterated in several ways, each with different trade-offs. This article covers six approaches: the classic index-based for-loop, the enhanced for-each loop, Iterator, ListIterator (which supports bidirectional traversal and in-place modification), forEach() with a lambda (Java 8+), and the Stream API.

Iterate through List using Regular for loop

The classic for loop gives you full control over iteration: you manage the index yourself, so you can start anywhere, end early, step by two, or go backwards. It is the right choice when you need the position of each element — for example, to compare adjacent items, to update specific indices, or to iterate only over a slice of the list. The loop runs from index 0 to list.size() - 1; accessing any index outside that range throws an IndexOutOfBoundsException.

Java
public static void iterateUsingRegularForLoop() {
    List<String> groceries = List.of("Tomatoes", "Potato", "Eggplant","Broccoli", "Squash");
    for(int i=0; i<groceries.size(); i++) { // regular for loop
        System.out.println("#Item : "+ groceries.get(i));
    }
}

Iterate through List using Enhanced for loop

  • The enhanced for loop syntax was introduced in Java 5 as simple way to iterate through List's elements. This syntax can be used to iterate from first index to last index if you do not need to know the index of current element. If you need to know the index, you can use regular for loop.
  • Enhanced for loop syntax looks like this, for (Object obj : list) { }, here Object obj is the element type which the list holds.
Java
public static void iterateUsingEnhancedForLoop() {
    List<String> groceries = List.of("Tomatoes", "Potato", "Eggplant","Broccoli", "Squash");
    for (String item : groceries) { // enhanced for loop syntax
        System.out.println("#Item : " + item);
    }
}

Iterate through List using forEach method

Iterate through List using Iterator

Java
package io.cscode.collections.list.examples;

import java.util.Iterator;
import java.util.List;

public class IterateThroughListUsingIterator {

    public static void iterateUsingIterator() {

        List<String> groceries = List.of("Tomatoes", "Potato", "Eggplant","Broccoli", "Squash");
        //iterator works based on a pointer hasNext() will tell whether it reached end of List or not
        // and next() will fetch the element and move pointer to next element if there is any
        System.out.println("###### iterator in while loop #####");
        Iterator<String> ite = groceries.iterator();
        while(ite.hasNext()) {
            System.out.println("inside while loop : "+ ite.next());
        }

        // same above logic can be implemented using while loop as well.
        System.out.println("###### iterator in for loop #####");
        for(Iterator<String> it = groceries.iterator(); it.hasNext();) {
            System.out.println("inside for loop : "+ it.next());
        }

    }

    public static void main(String[] args) {
        iterateUsingIterator();
    }
}

Iterate through List using a while loop

You can loop through elements of List using a while with a condition that checks whether we reach end of list or not. In below example we are using an integer counter to check end of loop.

Java
public static void iterateUsingWhileLoop(){
List<String> groceries = List.of("Tomatoes", "Potato", "Eggplant","Broccoli", "Squash");
    int counter =0;
    while(counter<groceries.size()) {
        System.out.println("#Item : " + groceries.get(counter++));
    }
}

Similar Articles

  1. How to remove elements from a List
  2. How to remove elements from a List while iterating