方式一 通过Map.keySet使用iterator遍历
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Test
public void testHashMap1() {
Map<Integer, String> map = new HashMap<>();
map.put(001, "Java");
map.put(002, "数据库");
map.put(003, "Vue");
System.out.println(map);
// 通过Map.keySet使用iterator遍历key,然后通过key得到对应的value值
Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
Integer key = iterator.next();
String value = map.get(key);
System.out.println("key = " + key + ", value = " + value);
}
}
|
结果:
{1=Java, 2=数据库, 3=Vue}
key = 1, value = Java
key = 2, value = 数据库
key = 3, value = Vue
方式二 通过Map.entrySet使用iterator遍历
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Test
public void testHashMap2() {
Map<Integer, String> map = new HashMap<>();
map.put(001, "Java");
map.put(002, "数据库");
map.put(003, "Vue");
System.out.println(map);
// 通过Map.entrySet使用iterator遍历key和value;注意 Set entrySet():返回所有key-value对构成的Set集合
Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, String> entry = entries.next();
System.out.println(entry);
}
}
|
结果:
方式三 通过Map.keySet遍历
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Test
public void testHashMap3() {
Map<Integer, String> map = new HashMap<>();
map.put(001, "Java");
map.put(002, "数据库");
map.put(003, "Vue");
System.out.println(map);
// 通过Map.keySet遍历key,然后通过key得到对应的value值
for (Integer key : map.keySet()) {
System.out.println("key = " + key + ", value = " + map.get(key));
}
}
|
结果:
{1=Java, 2=数据库, 3=Vue}
key = 1, value = Java
key = 2, value = 数据库
key = 3, value = Vue
方式四 通过For-Each迭代entries,使用Map.entrySet遍历
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Test
public void testHashMap4() {
Map<Integer, String> map = new HashMap<>();
map.put(001, "Java");
map.put(002, "数据库");
map.put(003, "Vue");
System.out.println(map);
// 使用For-Each迭代entries,通过Map.entrySet遍历key和value
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
}
|
{1=Java, 2=数据库, 3=Vue}
key = 1, value = Java
key = 2, value = 数据库
key = 3, value = Vue
方式五 使用lambda表达式forEach遍历
|
1
2
3
4
5
6
7
8
9
10
11
|
@Test
public void testHashMap5() {
Map<Integer, String> map = new HashMap<>();
map.put(001, "Java");
map.put(002, "数据库");
map.put(003, "Vue");
System.out.println(map);
// 使用lambda表达式forEach遍历
map.forEach((k, v) -> System.out.println("key = " + k + ", value = " + v));
}
|
forEach 源码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
default void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action);
for (Map.Entry<K, V> entry : entrySet()) {
K k;
V v;
try {
k = entry.getKey();
v = entry.getValue();
} catch(IllegalStateException ise) {
// this usually means the entry is no longer in the map.
throw new ConcurrentModificationException(ise);
}
action.accept(k, v);
}
}
|
从源码可以看到,这种新特性就是在传统的迭代方式上加了一层壳,但是让代码变得更加简单。(开发中推荐使用)
总结
推荐使用 entrySet 遍历 Map 类集合 KV (文章中的第四种方式),而不是 keySet 方式进行遍历。
keySet 其实是遍历了 2 次,第一次是转为 Iterator 对象,第二次是从 hashMap 中取出 key 所对应的 value值。而 entrySet 只是遍历了一次,就把 key 和 value 都放到了 entry 中,效率更高。
values()返回的是 V 值集合,是一个 list 集合对象;keySet()返回的是 K 值集合,是一个 Set 集合对象;entrySet()返回的是 K-V 值组合集合。
如果是 JDK8,推荐使用Map.forEach 方法(文章中的第五种方式)。
到此这篇关于Java中遍历Map集合的5种方式的文章就介绍到这了,更多相关Java遍历Map集合内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://blog.csdn.net/weixin_43570367/article/details/113336560
相关文章
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-06-04 21
-
2025-05-24 48
-
2025-05-27 102
-
2025-06-04 47
-
2025-05-25 74

