Iterate through List elements

Tags:

Introduction

In this article we are going to look at different ways of iterating List elements in Java.

Iterate through List using Regular for loop

Below is an example code to loop through List using regular for loop.


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.

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


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.


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