Backend Development 10 min read

Various Ways to Iterate Over a Java HashMap with Code Examples

This article demonstrates seven common techniques for iterating over a Java HashMap—including iterator, entrySet, keySet, for‑each loops, lambda expressions, and both single‑threaded and parallel Streams API—providing concise code examples and performance notes for each method.

Top Architect
Top Architect
Top Architect
Various Ways to Iterate Over a Java HashMap with Code Examples

The article introduces seven categories of HashMap traversal in Java, summarizing the typical approaches and their variations.

1. Iterator (EntrySet) – considered the most efficient method.

@Test
//1,使用迭代器 EntrySet 的方式遍历
public void demo1(){
    //创建Map 对象
    Map
map = new HashMap<>();
    //添加数据
    map.put(1,"娇娇");
    map.put(2,"娇娇1");
    map.put(3,"娇娇2");
    map.put(4,"娇娇3");
    map.put(5,"娇娇4");
    map.put(5,"娇娇5");
    //遍历
    Iterator
> iterator = map.entrySet().iterator();
    while(iterator.hasNext()){
        Map.Entry
next = iterator.next();
        System.out.println(next.getKey());
        System.out.println(next.getValue());
    }
}

2. Iterator (KeySet) – iterates over keys and retrieves values.

@Test
// 2,使用迭代器的KeySet
public void demo1(){
    //创建Map 对象
    Map
map = new HashMap<>();
    //添加数据
    map.put(1,"娇娇");
    map.put(2,"娇娇1");
    map.put(3,"娇娇2");
    map.put(4,"娇娇3");
    map.put(5,"娇娇4");
    map.put(5,"娇娇5");
    //遍历
    Iterator
iterator = map.keySet().iterator();
    while(iterator.hasNext()){
        Integer key = iterator.next();
        System.out.print(key);
        System.out.print(map.get(key));
    }
}

3. For‑Each loop with EntrySet.

@Test
//3,使用 For Each EntrySet 的方式进行遍历;
public void demo1(){
    //创建Map 对象
    Map
map = new HashMap<>();
    //添加数据
    map.put(1,"娇娇");
    map.put(2,"娇娇1");
    map.put(3,"娇娇2");
    map.put(4,"娇娇3");
    map.put(5,"娇娇4");
    map.put(5,"娇娇5");
    //遍历
    for (Map.Entry
entry: map.entrySet()) {
        System.out.println("entry.getKey() = " + entry.getKey());
        System.out.println("entry.getValue() = " + entry.getValue());
    }
}

4. For‑Each loop with KeySet.

@Test
//4,使用 For Each KeySet 的方式进行遍历;
public void demo1(){
    //创建Map 对象
    Map
map = new HashMap<>();
    //添加数据
    map.put(1,"娇娇");
    map.put(2,"娇娇1");
    map.put(3,"娇娇2");
    map.put(4,"娇娇3");
    map.put(5,"娇娇4");
    map.put(5,"娇娇5");
    //遍历
    for (Integer key: map.keySet()) {
        System.out.println(key);
        System.out.println(map.get(key));
    }
}

5. Lambda expression with forEach.

@Test
//5,使用 Lambda 表达式的方式进行遍历;
public void demo1(){
    //创建Map 对象
    Map
map = new HashMap<>();
    //添加数据
    map.put(1, "娇娇");
    map.put(2, "娇娇1");
    map.put(3, "娇娇2");
    map.put(4, "娇娇3");
    map.put(5, "娇娇4");
    map.put(5, "娇娇5");
    //遍历
    map.forEach((key,value) -> {
        System.out.print(key);
        System.out.print(value);
    });
}

6. Single‑threaded Streams API.

@Test
//6,使用 Streams API 单线程的方式进行遍历;
public void demo1(){
    //创建Map 对象
    Map
map = new HashMap<>();
    //添加数据
    map.put(1, "娇娇");
    map.put(2, "娇娇1");
    map.put(3, "娇娇2");
    map.put(4, "娇娇3");
    map.put(5, "娇娇4");
    map.put(5, "娇娇5");
    //遍历
    map.entrySet().stream().forEach((integerStringEntry -> {
        System.out.println(integerStringEntry.getKey());
        System.out.println(integerStringEntry.getValue());
    }));
}

7. Parallel Streams API for multi‑threaded traversal.

@Test
//6,使用 Streams API 单线程的方式进行遍历;
public void demo1(){
    //创建Map 对象
    Map
map = new HashMap<>();
    //添加数据
    map.put(1, "娇娇");
    map.put(2, "娇娇1");
    map.put(3, "娇娇2");
    map.put(4, "娇娇3");
    map.put(5, "娇娇4");
    map.put(5, "娇娇5");
    //遍历
    map.entrySet().parallelStream().forEach((integerStringEntry -> {
        System.out.println(integerStringEntry.getKey());
        System.out.println(integerStringEntry.getValue());
    }));
}

Each method is accompanied by execution results (images in the original article) and brief comments about efficiency. The tutorial concludes with an invitation for readers to discuss, ask questions, and explore related resources.

BackendJavalambdaHashMapCode ExampleStreams 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

login 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.