Fundamentals 7 min read

Five Best Ways to Iterate a Java HashMap

This article demonstrates five optimal techniques for traversing a Java HashMap—including using an Iterator over the entry set or key set, a for‑each loop, Lambda expressions, and the Stream API—providing complete code examples and sample outputs for each method.

Top Architect
Top Architect
Top Architect
Five Best Ways to Iterate a Java HashMap

In this article we discuss five best ways to iterate a Java HashMap.

Using Iterator to traverse HashMap EntrySet

Using Iterator to traverse HashMap KeySet

Using For‑each loop to iterate HashMap

Using Lambda expression to traverse HashMap

Using Stream API to traverse HashMap

1. Using Iterator to traverse HashMap EntrySet

package com.java.tutorials.iterations;

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

/**
 * 在 Java 中遍历 HashMap 的5种最佳方法
 * @author Ramesh Fadatare
 */
public class IterateHashMapExample {
    public static void main(String[] args) {
        // 1. 使用 Iterator 遍历 HashMap EntrySet
        Map<Integer, String> coursesMap = new HashMap<Integer, String>();
        coursesMap.put(1, "C");
        coursesMap.put(2, "C++");
        coursesMap.put(3, "Java");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "Hibernate ORM framework");

        Iterator<Entry<Integer, String>> iterator = coursesMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<Integer, String> entry = iterator.next();
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

Output:

1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework

2. Using Iterator to traverse HashMap KeySet

package com.java.tutorials.iterations;

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

/**
 * 在 Java 中遍历 HashMap 的5种最佳方法
 * @author Ramesh Fadatare
 */
public class IterateHashMapExample {
    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, "Spring Framework");
        coursesMap.put(5, "Hibernate ORM framework");

        // 2. 使用 Iterator 遍历 HashMap KeySet
        Iterator<Integer> iterator = coursesMap.keySet().iterator();
        while (iterator.hasNext()) {
            Integer key = iterator.next();
            System.out.println(key);
            System.out.println(coursesMap.get(key));
        }
    }
}

Output:

1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework

3. Using For‑each loop to iterate HashMap

package com.java.tutorials.iterations;

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

/**
 * 在 Java 中遍历 HashMap 的5种最佳方法
 * @author Ramesh Fadatare
 */
public class IterateHashMapExample {
    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, "Spring Framework");
        coursesMap.put(5, "Hibernate ORM framework");

        // 3. 使用 For-each 循环遍历 HashMap
        for (Map.Entry<Integer, String> entry : coursesMap.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

Output:

1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework

4. Using Lambda expression to traverse HashMap

package com.java.tutorials.iterations;

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

/**
 * 在 Java 中遍历 HashMap 的5种最佳方法
 * @author Ramesh Fadatare
 */
public class IterateHashMapExample {
    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, "Spring Framework");
        coursesMap.put(5, "Hibernate ORM framework");

        // 4. 使用 Lambda 表达式遍历 HashMap
        coursesMap.forEach((key, value) -> {
            System.out.println(key);
            System.out.println(value);
        });
    }
}

Output:

1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework

5. Using Stream API to traverse HashMap

package com.java.tutorials.iterations;

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

/**
 * 在 Java 中遍历 HashMap 的5种最佳方法
 * @author Ramesh Fadatare
 */
public class IterateHashMapExample {
    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, "Spring Framework");
        coursesMap.put(5, "Hibernate ORM framework");

        // 5. 使用 Stream API 遍历 HashMap
        coursesMap.entrySet().stream().forEach((entry) -> {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });
    }
}

Output:

1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaLambdaHashMapIteratorStream APIiteration
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.