How to Iterate Over Collections In Java

How to Iterate Over Collections In Java

2 Comments

Last Updated on October 21, 2024 by jt

Iteration is the most common operations done on a collection. For example, you may want to go through all the student grades to find who has the highest test scores.

In Java we have four methods for iterating over a collection, the methods are the for loop, iterator, forEach and lambda expressions.

Setup Collection

Here we create an ArrayList with the names of some employees.

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

public class Main {

    public static void main(String[] args) {

        List<String> names = new ArrayList<>();
        names.add("Harry");
        names.add("John");
        names.add("Bill");
    }
}

Iterating With A For Loop

This is the first method that most developers learn when starting to program. This method uses a counter variable named index and moves from the first item in collection to the last item in the collection.

public static void printList(List<String> list) {

    for (int index = 0; index < list.size(); index++) {

        // Get String at index;
        String name = list.get(index);
        // print name;
        System.out.println(name);
    }
}

The only downside to using this method is that it cannot be used on collections like Set. This is because Set is not an index base collection.

Output:

Harry
John
Bill

Iterating With An Iterator

This method loops over the items in the collection and uses the next method to get the item.

public static void printList(List<String> list) {

    for (int index = 0; index < list.size(); index++) {

        // Get String at index;
        String name = list.get(index);
        // print name;
        System.out.println(name);
    }
}

Output:

Harry
John
Bill

Iterating With The Enhanced For Loop

The enhanced for loop is using the iterator method behind the scenes. Using the technique is much easier to read and is used more often.

public static void printList(List<String> list) {

    for (String name : list) {
        System.out.println(name);
    }
}

Output:

Harry
John
Bill

Iterating With The ForEach

With this method, the developer does not have to write code to iterate over the collection because Java does it for us. Instead, we specify what to do in each iteration.

public static void printList(List<String> list) {

    list.forEach(name -> System.out.println(name));
}

Output:

Harry
John
Bill

Conclusion

You have seen different ways to iterate over a collection. Nowadays the Enhanced For Loop and the forEach methods are the most simple way for iterating over collections in Java.

Originally published at fluentjava.com

About Harrison Brock

Java and Go Software Engineer.

You May Also Like

2 comments on “How to Iterate Over Collections In Java

  1. February 9, 2019 at 10:07 am

    Thanks,sir

    Reply
  2. March 28, 2019 at 3:40 am

    Iterator method does not have an iterator 😉

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.