How to iterate through Java List?


The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements. User of a list has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable. ArrayList is the most popular implementation of the List interface.

Java List can be iterated using multiple ways. In this article, we're going to discuss various ways of iterating the list and corresponding examples.

Way #1

Use for/while loop to iterate a List and get element by index.

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

Way #2

Use foreach loop to iterate list of elements.

for (Integer integer : list) {
   System.out.print(integer + " ");
}

Way #3

Use iterator from the list to iterate through its elements.

Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()) {
   System.out.print(iterator.next() + " ");
}

Way #4

Use listIterator from the list to iterate through its elements.

Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()) {
   System.out.print(iterator.next() + " ");
}

Way #5

Use forEach of the list to iterate through its elements.

list.forEach(i -> {System.out.print(i + " ");});

Way #6

Use forEach of the stream of list to iterate through its elements.

list.stream().forEach(i -> {System.out.print(i + " ");});

Example 1

Following is the example showing loops and iterator methods to iterate a list −

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class CollectionsDemo {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));
      for(int i= 0; i< list.size(); i++) {
         System.out.print(list.get(i) + " ");
      }
      System.out.println();
      for (Integer integer : list) {
         System.out.print(integer + " ");
      }
      Iterator<Integer> iterator = list.iterator();
      System.out.println();
      while(iterator.hasNext()) {
         System.out.print(iterator.next() + " ");
      }
   }
}

Output

This will produce the following result −

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

Example 2

Following is another example showing listIterator, forEach and streams methods to iterate a list −

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class CollectionsDemo {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));
      Iterator<Integer> listIterator = list.listIterator();
     
      while(listIterator.hasNext()) {
         System.out.print(listIterator.next() + " ");
      }
      System.out.println();
      list.forEach(i -> {System.out.print(i + " ");});
     
      System.out.println();
      list.stream().forEach(i -> {System.out.print(i + " ");});
   }
}

Output

This will produce the following result −

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

Updated on: 26-May-2022

432 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements