3 Examples to Loop Through a List in Java 8

3 ways to Loop through a List in Java
There are multiple ways to traverse or loop through a List in Java e.g. by using an Iterator, by using an enhanced for loop of Java 5, and not the forEach() method of Java 8. Given a List is an index-based collection if you know the index you can retrieve an object from List and because of this, you can also use traditional for loop which keeps count for iterating List. Now the question is whether should you use the Iterator or enhanced for loop, or the forEach() method of Java 8 for looping over List in Java. 

Well, it depends on what you are doing with the object, if you need to remove some objects from List than iterating using Iterator is the best choice to avoid ConcurrentModificationExceptionbut if you are not removing any element and just doing some operation with each element than enhanced for loop is much cleaner ways to do that. 

The main advantage of using enhanced for loop over Iterator is that you don't need to check for the next element like you need to in the case of Iterator, Java 5 advanced for loop keeps track of size. Also, the code is very clean with no boilerplate.

But, like everything else in the world, you won't get all benefit, you do have some limitations while loop through a List using enhanced for loop, as shown here

With the enhanced for loop, you can not modify selective objects as you don't have an index with you. If you want to selective modify only certain objects then your best bet is to use the traditional for loop which keeps track of indexes.

The third choice is very straightforward, if you are running on Java SE 8 version then there is no reason for not using the forEach() method for looping through a List in Java. It's lazy and allows you to perform some filtering operation on the stream before fetching an element from the list. 

The laziness comes from the Stream class itself.  You can check out The Complete Java Masterclass to learn more about performance benefits provided by lambda expression and stream in Java 8. 

3 Examples of looping through a List in Java

List ArrayList loop example JavaHere is a sample Java program that demonstrates How to loop through a List in Java in three different ways, Iterator, for-each loop, and traditional for loop. This technique can be used to loop through ArrayList or any other index-based List implementation e.g. Vector. 

The other two methods like Iterator and enhanced for loop can be used along with any Collection class like HashSet, TreeSet, LinkedHashSet, etc. They are actually the standard way to iterate through the collection in Java. 

Java List Iterator Example

The traditional for loop approach is only possible and efficient because of the index-based nature of the List interface, but if you use this with a linked list then you will get worse performance because in LinkedList accessing an element with an index is O(n) operation rather than O(1) operation. This is also the fundamental difference between an ArrayList and LinkedList in Java

The new method of iterating over a list using the forEach() method is only available in Java SE 8 and I recommend you to read this article to learn how to loop through a list using the forEach() method in Java 8 before you start using them.

Btw, Java SE 8 is full of exciting and more powerful features and it's essential for a Java developer to learn those features, given its the most popular Java version. If you are interested, you should follow a good Java 8 course like What's New in Java 8 on Pluralsight to learn quickly.

3 ways to loop through a list in Java 8

Alternatively, you can always choose the one from this list of good Java 8 books, earlier published by me. 

How to loop through a List in Java

package example;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
 * Java program to demonstrate different ways to loop,
 * iterate or traverse List in Java.
 * There are three examples in this program,
 * first examples show how to loop List
 * using Iterator, Second Example shows Looping over List
 * using advanced Java 5 for loop
 * while third and last examples demonstrate
 * use of traditional for loop for traversing over
 * a List in Java.
 * @author Javin Paul

public class ListLoopExample{
    public static void main(String args[]){
        //First example to iterate List in Java using Iterator
        List<String> languages = Arrays.asList("Java",
                          "C++", "Scala", "Groovy");

        //Getting Iterator from List in Java
        Iterator<String> iterator = languages.iterator();
        System.out.println("Iterating List in Java
                           using Iterator "

        //Iterating through all elements of List
        while (iterator.hasNext()) {
            System.out.printf("Current element in List
                           is %s %n"
, iterator.next());

        //Second example of Iterating over List in Java
        // using a foreach loop

        System.out.println("Looping List in Java using a
                             foreach loop"
        for (String city : languages) {
            System.out.println("List Element: " +  city);
        //Third example of Looping List using traditional for loop
        for(int i =0; i<languages.size(); i++){
            System.out.printf("programming language #%d in
                  List is : %s %n"
, i, languages.get(i) );

Iterating List in Java using Iterator
The current element in List is London
The current element in List is Tokyo
The current element in List is NewYork
The current element in List in Mumbai
Looping List in Java using a foreach loop
List Element: London
List Element: Tokyo
List Element: NewYork
List Element: Mumbai
City #0 in List is: London
City #1 in List is: Tokyo
City #2 in List is: NewYork
City #3 in List is: Mumbai

That’s all on How to iterate or loop over a List in Java. In this Java tutorial, we have seen an example of all three ways of looping List in Java. the first example demonstrates the use of Iterator with List, the second use for loop for looping over List, and the third example uses traditional old for loop. All this technique can be applied to any index-based List implementation including ArrayList and Vector in Java.

Further Learning
The Complete Java Masterclass
Java Fundamentals: Collections
Java Programming Interview Exposed by Markham

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.


  1. the output results differ from the items that you have in the array, which confuses a little bit.

  2. I think there is a %n in the printf of the first loop that should not be there as the printf has only a parameter

  3. Can you please elaborate the use of foreach loop? Better if you show with example.


Feel free to comment, ask questions if you have any doubt.