Different ways to iterate/loop a List of Integers in Java


In Java, you can iterate or loop a List object in different ways. In this short example, you will find 8 different ways to iterate/loop a List of Integers in Java.

P.S. Tested with up to JDK 8 version.

Suppose you have the following list object:

final List<Integer> integerList = Arrays.asList(101, 33, 9, 55, 76, 22, 95);

1. For Loop

For loop is one of the oldest/legacy techniques for iterating a list of integers.

for (int i = 0; i < integerList.size(); i++) {
  System.out.println(integerList.get(i));
}

2. While Loop

int ctr = 0;

while (ctr < integerList.size()) {
  System.out.println(integerList.get(ctr));
  ctr++;
}

3. Iterator

Iterator<Integer> itr = integerList.iterator();

while (itr.hasNext()) {
  System.out.println(itr.next());
}

4. ListIterator

ListIterator<Integer> listIterator = integerList.listIterator();

while (listIterator.hasNext()) {
  System.out.println(listIterator.next());
}

5. Enhanced For Loop

for (Integer i : integerList) {
  System.out.println(i);
}

6. forEach with Anonymous Inner Class

Consumer is a functional interface introduced in Java 8. It accepts a single input argument and returns no result.

integerList.forEach(new Consumer<Integer>() {
    @Override
    public void accept(Integer integer) {
      System.out.println(integer);
    }
});

7. forEach with Lambda

Lambda Expressions introduced in Java 8. It is a short block of code that takes in parameters and returns a value.

integerList.forEach((Integer i) -> System.out.println(i));

// or 

integerList.forEach((i) -> System.out.println(i));

// or

integerList.forEach(i -> System.out.println(i));

8. forEach with Method Reference

Sometimes, a lambda expression does nothing but call an existing method. In those cases, it’s often clearer to refer to the existing method by name. Method references enable you to do this; they are compact, easy-to-read lambda expressions for methods that already have a name.

integerList.forEach(System.out::println);

See the complete example.

IterateListOfIntegers.java
package org.websparrow.collections;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.function.Consumer;

public class IterateListOfIntegers {

  public static void main(String[] args) {

    final List<Integer> integerList = Arrays.asList(101, 33, 9, 55, 76, 22, 95);

    // 1. Legacy style - for loop
    for (int i = 0; i < integerList.size(); i++) {
      System.out.println(integerList.get(i));
    }

    // 2. while loop
    int ctr = 0;
    while (ctr < integerList.size()) {
      System.out.println(integerList.get(ctr));
      ctr++;
    }

    // 3. Iterator
    Iterator<Integer> itr = integerList.iterator();
    while (itr.hasNext()) {
      System.out.println(itr.next());
    }

    // 4. ListIterator
    ListIterator<Integer> listIterator = integerList.listIterator();
    while (listIterator.hasNext()) {
      System.out.println(listIterator.next());
    }

    // 5. Enhanced ForLoop
    for (Integer i : integerList) {
      System.out.println(i);
    }

    // 6. forEach with Anonymous Inner Class
    integerList.forEach(
        new Consumer<Integer>() {
          @Override
          public void accept(Integer integer) {
            System.out.println(integer);
          }
        });

    // 7. forEach with Lambda expression
    integerList.forEach((Integer i) -> System.out.println(i));
    // or
    integerList.forEach((i) -> System.out.println(i));
    // or
    integerList.forEach(i -> System.out.println(i));

    // 8. forEach with method reference
    integerList.forEach(System.out::println);
  }
}

Output:

101
33
9
55
76
22
95

References

  1. How to iterate ArrayList in Java
  2. How to iterate Enum in Java
  3. ListIterator (Java Platform SE 7 )
  4. Java 8 forEach JavaDoc
  5. Iterator (Java Platform SE 7 )

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.