Different Ways to Iterate over List, Set and Map in Java [Snippet]

In this quick article, we will discuss different ways to iterate over a List, Set and Map in Java 7 and 8.
Learn Collections Framework in-depth at Java Collections Tutorial

What will we learn?

  1. Different ways to iterate over a List
  2. Different Ways to Iterate over a Set 
  3. Different Ways to Iterate over a Map

1. Different ways to iterate over a List

Let's create a Java program to demonstrate different ways to iterate over a List using JDK 7 and 8. 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

2. Different Ways to Iterate over a Set

Let's create a Java program to demonstrate different ways to iterate over a Set using JDK 7 and 8. Please refer the comments in below program are self-descriptive.
package net.javaguides.collections;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Java program to demonstrate different ways to iterate over a Set in Java
 * 
 * @author Ramesh Fadatare
 *
 */
public class DifferentWaysSetIterateProgram {

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

        Set < String > courses = new HashSet < String > ();
        courses.add("Java");
        courses.add("C");
        courses.add("C++");
        courses.add("Python");
        courses.add("Scala");

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

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

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

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

        // JDK 8 streaming example method reference
        courses.stream().forEach(DifferentWaysSetIterateProgram::coursePrinter);

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

        // JDK 8 for each
        courses.forEach(DifferentWaysSetIterateProgram::coursePrinter);
    }

    // common method to print course
    private static void coursePrinter(String course) {
        System.out.println("course name :: " + course);
    }
}
Output:
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python

3. Different Ways to Iterate over a Map

Let's create a Java program to demonstrate different ways to iterate over a Map using JDK 7 and 8. Please refer the comments in below program are self-descriptive.
package net.javaguides.collections;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * This program demonstrate different ways to iterate over a Map in Java
 * 
 * @author Ramesh Fadatare
 *
 */
public class DifferentWaysMapIterateProgram {

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

        Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
        coursesMap.put(1, "C");
        coursesMap.put(2, "C++");
        coursesMap.put(3, "Java");
        coursesMap.put(4, "J2EE");
        coursesMap.put(5, "Python");
        coursesMap.put(6, "Scala");

        // Using iterator
        Iterator < Map.Entry < Integer, String >> iterator = coursesMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry < Integer, String > course = iterator.next();
            coursePrinter(course.getKey(), course.getValue());
        }

        // Enhanced for loop
        for (Map.Entry < Integer, String > beer: coursesMap.entrySet()) {
            coursePrinter(beer.getKey(), beer.getValue());
        }

        // JDK 8 for each with lambda
        coursesMap.forEach((k, v) - > coursePrinter(k, v));

        // JDK 8 for each method reference
        coursesMap.forEach(DifferentWaysMapIterateProgram::coursePrinter);

    }

    // common method to print map key value
    private static void coursePrinter(Integer number, String brand) {
        System.out.println("course no : " + number + " and course name : " + brand);
    }
}
Output:
course no : 1 and course name : C
course no : 2 and course name : C++
course no : 3 and course name : Java
course no : 4 and course name : J2EE
course no : 5 and course name : Python
course no : 6 and course name : Scala
course no : 1 and course name : C
course no : 2 and course name : C++
course no : 3 and course name : Java
course no : 4 and course name : J2EE
course no : 5 and course name : Python
course no : 6 and course name : Scala
course no : 1 and course name : C
course no : 2 and course name : C++
course no : 3 and course name : Java
course no : 4 and course name : J2EE
course no : 5 and course name : Python
course no : 6 and course name : Scala
course no : 1 and course name : C
course no : 2 and course name : C++
course no : 3 and course name : Java
course no : 4 and course name : J2EE
course no : 5 and course name : Python
course no : 6 and course name : Scala

Similar Collections Examples [Snippet]

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments