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.
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<Integer, String> 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<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Integer, String> 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<Integer, String> 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<Integer> 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<Integer, String> 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<Integer,String> 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<Integer, String> 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<Integer, String> 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<Integer, String> 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<Integer, String> 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
