Different Ways to Iterate over a Map in Java [Snippet]

In this quick article, we will discuss different ways to iterate over a Map in Java. Iterating or looping a Map 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 Map using JDK 7 and 8.
Learn Collections Framework in-depth at Java Collections Tutorial

java.util.Map<Integer, String>

A Map has a Set of key-value pairs. One key refers to only one value as such a map cannot contain duplicate keys.
Read more about Map interface at Collections Framework - The Map Interface

Java Program to Demonstrate Different Ways to Iterate over a Map 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.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
Please leave a comment if you have any other way to iterate over a Map.

Similar Collections Examples [Snippet]


Comments