Different Ways to Iterate over a List in Java

In this quick article, we will discuss different ways to iterate over a List in Java. Iterating or looping a List is a common task in a day to day project work so in this article, I would like to demonstrate different ways to iterate over a List using JDK 7 and 8.
Learn Collections Framework in-depth at Java Collections Tutorial

Java Program to iterate over a List in Java

Note that I have used lambda expression and method reference features in this program so you have to use JDK 8 or later to run this program. Please refer the comments in below program are self-descriptive.
package net.javaguides.collections;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * Java program to demonstrate different ways to Iterate over an ArrayList in Java
 * @author Ramesh Fadatare
 *
 */
public class DifferentWaysListIterateProgram {

    public static void main(String...args) {

        List < String > courses = Arrays.asList("C", "C++", "Core Java", "J2EE", "Spring", "Hibernate", "Python");

        // Basic loop
        for (int i = 0; i < courses.size(); i++) {
            String course = courses.get(i);
            printCourse(course);
        }

        // Enhanced for loop
        for (String course: courses) {
            printCourse(course);
        }

        // Basic loop with iterator
        for (Iterator < String > it = courses.iterator(); it.hasNext();) {
            String course = it.next();
            printCourse(course);
        }

        // Iterator with while loop
        Iterator < String > it = courses.iterator();
        while (it.hasNext()) {
            String course = it.next();
            printCourse(course);
        }

        // JDK 8 streaming example lambda expression
        courses.stream().forEach(course - > printCourse(course));

        // JDK 8 streaming example method reference
        courses.stream().forEach(DifferentWaysListIterateProgram::printCourse);

        // JDK 8 for each with lambda
        courses.forEach(course - > printCourse(course));

        // JDK 8 for each
        courses.forEach(DifferentWaysListIterateProgram::printCourse);
    }

    // common method to print course
    private static void printCourse(String course) {
        System.out.println("course name :: " + course);
    }
}
Output:
course name :: C
course name :: C++
course name :: Core Java
course name :: J2EE
course name :: Spring
course name :: Hibernate
course name :: Python
course name :: C
course name :: C++
course name :: Core Java
course name :: J2EE
course name :: Spring
course name :: Hibernate
course name :: Python
course name :: C
course name :: C++
course name :: Core Java
course name :: J2EE
course name :: Spring
course name :: Hibernate
course name :: Python
course name :: C
course name :: C++
course name :: Core Java
course name :: J2EE
course name :: Spring
course name :: Hibernate
course name :: Python
course name :: C
course name :: C++
course name :: Core Java
course name :: J2EE
course name :: Spring
course name :: Hibernate
course name :: Python
course name :: C
course name :: C++
course name :: Core Java
course name :: J2EE
course name :: Spring
course name :: Hibernate
course name :: Python
course name :: C
course name :: C++
course name :: Core Java
course name :: J2EE
course name :: Spring
course name :: Hibernate
course name :: Python
course name :: C
course name :: C++
course name :: Core Java
course name :: J2EE
course name :: Spring
course name :: Hibernate
course name :: Python
Please leave a comment if you have any other way to iterate over a Set so that I will add to this article.

Related Collections Examples

Comments