How to Loop Over ArrayList in Java

Amir
Amir
How to Loop Over ArrayList in Java

Looping over an ArrayList in Java. In this tutorial, we look at five different ways we can iterate through an ArrayList in Java. As of Java 8, we can use the forEach method as well as the iterator class to loop over an ArrayList.

Looping over an ArrayList

There are primarily 5 different ways to loop over an ArrayList

  1. Classic For Loop
  2. Advanced For Loop
  3. Iterator
  4. While Loop
  5. ForEach (Java 8)

First, let’s create an ArrayList to use in the loop examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.testingexcellence.tutorials;

import java.util.ArrayList;
import java.util.List;

public class LoopOverArrayExamples {

    private List fruitBasket = new ArrayList<>(0);

    public void addFruitsToBasket() {
        fruitBasket.add("Apple");
        fruitBasket.add("Banana");
        fruitBasket.add("Pear");
        fruitBasket.add("Mango");
    }
}

Using Classic For Loop

1
2
3
for (int i=0; i<fruitBasket.size(); i++) {
    System.out.println(fruitBasket.get(i));
}

Advanced For Loop

1
2
3
for(String fruit : fruitBasket) {
    System.out.println(fruit);
}

Using Iterator

1
2
3
4
Iterator fruitIterator = fruitBasket.iterator();
while (fruitIterator.hasNext()) {
    System.out.println(fruitIterator.next());
}

Using While Loop

1
2
3
4
5
int i = 0;
while (i < fruitBasket.size()) {
    System.out.println(fruitBasket.get(i));
    i++;
}

ForEach (Java 8)

1
2
3
fruitBasket.forEach( (fruit) ->
    System.out.println(fruit)
);
See also