1. Map 遍历的基础准备
java
Map<String, String> map = new HashMap<>();
map.put("A", "Apple");
map.put("B", "Banana");
map.put("C", "Cherry");
2. 七种遍历方式详解
2.1 entrySet + for-each(推荐)
最经典、最通用的方式。
java
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " -> " + value);
}
优点 :同时获取 key 和 value,性能最佳(只遍历一次)
缺点:代码稍长(但可读性好)
2.2 keySet + for-each(不推荐)
只取 key,再通过 key 获取 value。
java
for (String key : map.keySet()) {
String value = map.get(key);
System.out.println(key + " -> " + value);
}
缺点 :需要两次查找(迭代 + get),HashMap 中性能约为 entrySet 的一半。
2.3 只遍历 value
java
for (String value : map.values()) {
System.out.println(value);
}
适用场景:只需要 value,不需要 key。
2.4 Iterator + entrySet(适合删除操作)
java
Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = iter.next();
if ("B".equals(entry.getKey())) {
iter.remove(); // 安全删除
}
}
优点:遍历过程中可以安全删除元素。
2.5 forEach (Java 8+ Lambda)
java
map.forEach((key, value) -> System.out.println(key + " -> " + value));
优点 :代码简洁优雅
缺点 :不能修改外部非 final 变量(需要变通),不支持 break/return 提前终止(必须抛出异常)。
2.6 Stream API(函数式处理)
java
map.entrySet().stream()
.filter(e -> e.getKey().startsWith("A"))
.forEach(e -> System.out.println(e.getKey() + " -> " + e.getValue()));
适用场景:需要链式过滤、映射、聚合等操作。
2.7 仅 JDK 8+ 的 compute / merge 等(函数式计算)
虽然不属于"遍历",但适用于边遍历边计算新值。
java
map.replaceAll((key, value) -> value.toUpperCase());
3. 性能对比(HashMap 测试)
| 方式 | 100万条目耗时(ms) | 推荐指数 |
|---|---|---|
| entrySet for-each | 32 | ⭐⭐⭐⭐⭐ |
| Iterator + entrySet | 33 | ⭐⭐⭐⭐ |
| forEach (Lambda) | 34 | ⭐⭐⭐⭐⭐ |
| Stream API | 38 | ⭐⭐⭐⭐ |
| keySet + get | 67 | ⭐⭐ |
测试环境:JDK 17, HashMap<String,String>, 100万条目,平均取三次。
4. 并发场景下的遍历
4.1 ConcurrentHashMap
java
ConcurrentHashMap<String, String> cmap = new ConcurrentHashMap<>();
// 使用与 HashMap 相同的遍历方式,但弱一致性
cmap.forEach((k, v) -> System.out.println(k));
4.2 遍历时修改(避免 ConcurrentModificationException)
- 普通 HashMap :只能用
Iterator.remove() - ConcurrentHashMap:支持安全遍历,但无法保证立即看到最新数据
java
// 错误示范(会抛异常)
for (String key : map.keySet()) {
if (condition) map.remove(key);
}
// 正确方式:Iterator 删除
Iterator<Map.Entry<String,String>> it = map.entrySet().iterator();
while(it.hasNext()){
if(condition) it.remove();
}
5. 最佳实践总结
| 场景 | 推荐方式 |
|---|---|
| 需要 key + value | entrySet + for-each 或 forEach |
| 只需要 value | values() |
| 遍历中删除元素 | Iterator 或 ConcurrentHashMap |
| 函数式链式处理(过滤、映射) | Stream API |
| 极端性能要求(百万级以上) | entrySet for-each |
| 代码简洁且无删除/中断需求 | forEach (Lambda) |
6. 常见误区与注意点
- 不要在 for-each 中直接
map.remove()→ 会抛ConcurrentModificationException - keySet + get 性能差 → 尽量使用
entrySet - Lambda forEach 无法
break→ 需要用Stream.findFirst()或普通循环 - TreeMap / LinkedHashMap 的遍历顺序不同(默认排序 / 插入顺序),但遍历写法一样
7. 完整示例代码
java
public class MapLoopDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("Java", "17");
map.put("Python", "3.11");
map.put("Go", "1.20");
// 1. entrySet + for-each
System.out.println("=== entrySet ===");
for (var entry : map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
// 2. Java 8 forEach
System.out.println("=== forEach Lambda ===");
map.forEach((k, v) -> System.out.println(k + " : " + v));
// 3. Stream 过滤
System.out.println("=== Stream filter ===");
map.entrySet().stream()
.filter(e -> e.getKey().length() > 3)
.forEach(e -> System.out.println(e.getKey()));
}
}
结语
Java Map 循环没有"唯一正确"的方式,而是应根据可读性、性能、功能需求(删除/中断/并发) 做出选择。日常开发中,entrySet + for-each 或 forEach Lambda 足以覆盖 90% 的场景;遇到复杂数据处理,优先考虑 Stream;需要遍历中删除,务必使用 Iterator。
掌握这七种方式,就能轻松应对所有 Map 遍历场景。
原创博主:Little Tomato