当前位置:网站首页>Map common traversal methods - keySet and entrySet
Map common traversal methods - keySet and entrySet
2022-08-04 13:32:00 【Self-disciplined watermelon L】
1. Traverse using keySet
implementation steps:
(1) Use the method keySet() in the Map collection to take out all the keys of the Map collection and store them in a Set collection
(2) Traverse the set collection to obtain each key in the Map collection
(3) Through the method get(key) in the Map collection, find the value through the key
@Testpublic void testKeySet() {Map map = new ConcurrentHashMap<>(16);map.put("one",1);map.put("two",2);map.put("three",3);// 1. Enhance the for loop to traverse the keys in the mapfor (String key : map.keySet()) {// Find the corresponding value by keyInteger value = map.get(key);System.out.println(key + "->" +value);}System.out.println("-----------------------");// 2. Use an iterator to traverse (all keys in the map collection are taken out and stored in the Set collection)Iterator iteratorKey = map.keySet().iterator();while (iteratorKey.hasNext()){String key = iteratorKey.next();Integer value = map.get(key);System.out.println(key + "->" +value);}} 2. Use entrySet to traverse
implementation steps:
(1) Map.entrySet() maps the key value of the map to a Set collection
(2) The Map.entrySet iterator will generate an EntryIterator, and the returned instance is an object containing key/value pairs
(3)getKey(): returns the key of the key-value pair
(4)getValue(): returns the value of the key-value pair
@Testpublic void testEntrySet() {Map map = new ConcurrentHashMap<>(16);map.put("one",1);map.put("two",2);map.put("three",3);// The Map.entrySet iterator will generate an EntryIterator, and the returned instance is an object containing key/value pairs.// The iterator in keySet returns only the key object, and it also needs to get the value twice in the map.Therefore, entrySet is about twice as fast as keySet.Set> entries = map.entrySet();Iterator> entryIterator = entries.iterator();while (entryIterator.hasNext()){// Iterates out the object of each instance, including key-value pairsMap.Entry entry = entryIterator.next();String key = entry.getKey();Integer value = entry.getValue();System.out.println(key + "->" + value);}} 3. Comparison
(1) The value obtained by keySet can only be obtained by first obtaining the key
(2) entrySet can directly obtain Key and Value from the iterated instance object after iteration
(3) The traversal results of the two methods are the same. If the query speed is required, select entrySet(), and entrySet can improve performance.
边栏推荐
- Motion Regulations (18) - and check the basic questions - gang
- Utility function---string processing
- 荧光磷脂PEG衍生物之一磷脂-聚乙二醇-荧光素,Fluorescein-PEG-DSPE
- router---dynamic route matching
- 备份控制文件
- A discussion of integrated circuits
- 【LeetCode】38、外观数列
- Why don't young people like to buy Mengniu and Yili?
- Opencv学习之ORB特征提取和匹配
- How to play the Tower of Hanoi
猜你喜欢

持续交付(四)Jenkins多线程任务执行

PMP每日一练 | 考试不迷路-8.4(包含敏捷+多选)

新 Nsight Graph、Nsight Aftermath 版本中的性能提升和增强功能

漏洞复现 - - - Alibaba Nacos权限认证绕过
![[UML] Summary of Information System Analysis and Design Knowledge Points](/img/a2/32267c5bfdf8114c4c723278a1897c.png)
[UML] Summary of Information System Analysis and Design Knowledge Points

使用SQLServer复制数据库

leetcode 48. Rotate Image 旋转图像(Medium)
![[Niu Ke brush questions-SQL big factory interview questions] NO5. Analysis of a treasure store (e-commerce model)](/img/9f/33e782b93fcaa15359450e59a7233d.png)
[Niu Ke brush questions-SQL big factory interview questions] NO5. Analysis of a treasure store (e-commerce model)

MFC的相机双目标定界面设计

谁说 Mysql 单表最大 2000 W ?我硬要塞它 1 个亿
随机推荐
MySQL性能指标TPS\QPS\IOPS如何压测?
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
nVisual二次开发——第二章 nVisual API操作指南Swagger使用
密码设置有关方法:不能相同字母,不能为连续字符
Launcher app prediction
router---dynamic route matching
让Web页面中的编辑器支持黏贴或直接拖拽来添加图片「建议收藏」
How to play the Tower of Hanoi
SMART S7-200PLC串行自由口通讯(耐压测试仪)
[Niu Ke brush questions-SQL big factory interview questions] NO5. Analysis of a treasure store (e-commerce model)
Is the code more messy?That's because you don't use Chain of Responsibility!
文字编码 - XML 教程
redisTemplate存取List遇到的坑
TS---类型设置
为什么密码云服务平台是云时代的必然之选?
FreeConfig.h文件
秋招攻略秘籍,吃透25个技术栈Offer拿到手软
【自动微分实现】反向OO实现自动微分(Pytroch核心机制)
基于双层共识控制的直流微电网优化调度(Matlab代码实现)
"Lonely Walking on the Moon" is a powerful medicine, it can't cure the internal friction of happy twist