JavaMap两种遍历方式keySet与entrySet详解
作者:自律的西瓜L 时间:2022-04-17 14:36:29
1.使用keySet进行遍历
实现步骤:
(1)使用Map集合中的方法keySet(),把Map集合所有的key取出来,存储到一个Set集合中
(2)遍历set集合,获得Map集合中的每一个key
(3)通过Map集合中的方法get(key),通过key找到value
@Test
public void testKeySet() {
Map<String, Integer> map = new ConcurrentHashMap<>(16);
map.put("one",1);
map.put("two",2);
map.put("three",3);
// 1.增强for循环遍历map中的key
for (String key : map.keySet()) {
// 通过key找到对应的value
Integer value = map.get(key);
System.out.println(key + "->" +value);
}
System.out.println("------------------------");
// 2.使用迭代器来进行遍历(map集合中所有的key取出来,存储到Set集合中)
Iterator<String> iteratorKey = map.keySet().iterator();
while (iteratorKey.hasNext()){
String key = iteratorKey.next();
Integer value = map.get(key);
System.out.println(key + "->" +value);
}
}
2.使用entrySet进行遍历
实现步骤:
(1)Map.entrySet()把map的键值映射成Set集合
(2)Map.entrySet迭代器会生成EntryIterator,其返回的实例是一个包含key/value键值对的对象
(3)getKey(): 返回键值对的key
(4)getValue(): 返回键值对的value
@Test
public void testEntrySet() {
Map<String, Integer> map = new ConcurrentHashMap<>(16);
map.put("one",1);
map.put("two",2);
map.put("three",3);
// Map.entrySet迭代器会生成EntryIterator,其返回的实例是一个包含key/value键值对的对象。
// 而keySet中迭代器返回的只是key对象,还需要到map中二次取值。故entrySet要比keySet快一倍左右。
Set<Map.Entry<String, Integer>> entries = map.entrySet();
Iterator<Map.Entry<String, Integer>> entryIterator = entries.iterator();
while (entryIterator.hasNext()){
// 迭代出来的是每一个实例的对象,包含键值对
Map.Entry<String, Integer> entry = entryIterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + "->" + value);
}
}
3.对比
(1)keySet获取value只能通过先获取key才能得到value
(2)entrySet在迭代之后可以直接从迭代出来的实例对象中直接获取Key和Value
(3)两个方法遍历的结果都一样,若对查询速度有要求就选entrySet(),entrySet可以提高性能。
来源:https://blog.csdn.net/weixin_42218169/article/details/126143697
标签:Java,Map,keySet,entrySet
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在IntelliJ IDEA中为自己设计的类库生成JavaDoc的方法示例
2023-11-25 09:49:02
java实现单人版五子棋游戏
2021-09-03 03:24:20
![](https://img.aspxhome.com/file/2023/0/63060_0s.png)
用java开发dota英雄最华丽的技能(实例讲解)
2022-05-27 00:10:20
![](https://img.aspxhome.com/file/2023/8/62158_0s.jpg)
Java实现新建有返回值的线程的示例详解
2022-02-13 17:09:33
![](https://img.aspxhome.com/file/2023/0/61420_0s.jpg)
JAVA抽象类,接口,内部类详解
2023-11-09 16:37:25
关于springboot配置文件密文解密方式
2023-11-09 04:21:24
![](https://img.aspxhome.com/file/2023/8/59418_0s.png)
java并发编程包JUC线程同步CyclicBarrier语法示例
2023-01-18 07:44:58
![](https://img.aspxhome.com/file/2023/7/63687_0s.png)
java @Value(
2023-10-05 02:54:47
![](https://img.aspxhome.com/file/2023/7/63017_0s.png)
深入JAVA对象深度克隆的详解
2022-01-05 14:03:13
浅谈AnDroidDraw+DroidDraw实现Android程序UI设计的分析说明
2023-09-28 20:41:51
![](https://img.aspxhome.com/file/2023/1/85991_0s.jpg)
Android 消息机制问题总结
2023-08-06 03:10:49
![](https://img.aspxhome.com/file/2023/5/84715_0s.jpg)
springcloud LogBack日志使用详解
2021-12-23 15:17:05
![](https://img.aspxhome.com/file/2023/4/63764_0s.png)
Java8新特性:lambda表达式总结
2023-11-29 00:16:52
Java Timezone类常见问题_动力节点Java学院整理
2023-08-23 01:30:11
解决maven没有打包xml文件的问题
2023-11-27 14:51:01
springboot如何读取配置文件到静态工具类
2023-11-28 04:44:54
![](https://img.aspxhome.com/file/2023/6/60036_0s.png)
java编写ftp下载工具
2022-03-15 08:15:13
Java技能点之SimpleDateFormat进行日期格式化问题
2023-09-09 01:50:26
![](https://img.aspxhome.com/file/2023/9/58339_0s.jpg)
MyBatis自定义映射关系和关联查询实现方法详解
2021-12-18 21:25:28
![](https://img.aspxhome.com/file/2023/2/60902_0s.png)
三种Java自定义DNS解析器方法与实践
2022-01-13 10:12:11