当前位置:网站首页>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.
边栏推荐
- sqlserver删除重复数据
- 文字编码 - Markdown 简明教程
- (记录)异步并发,多线程处理表的统计
- nVisual secondary development - Chapter 2 nVisual API operation guide Swagger use
- 判断密码是否包含键盘连续字母
- Programmer Qixi Gift - How to quickly build an exclusive chat room for your girlfriend in 30 minutes
- odoo13笔记点
- RK1126编译gdb 板子上gdb调试程序
- 【自动微分实现】反向OO实现自动微分(Pytroch核心机制)
- A discussion of integrated circuits
猜你喜欢
企业应当实施的5个云安全管理策略
判断密码是否包含键盘连续字母
用过Apifox这个API接口工具后,确实感觉postman有点鸡肋......
汉诺塔怎么玩
MySQL-数据类型
双目立体视觉笔记(二)
How to stress the MySQL performance indicators TPS\QPS\IOPS?
PMP每日一练 | 考试不迷路-8.4(包含敏捷+多选)
CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔,可用于改善循环时间
[Niu Ke brush questions-SQL big factory interview questions] NO5. Analysis of a treasure store (e-commerce model)
随机推荐
RobotFramework二次开发(一)
LeetCode_299_猜数字游戏
一文梳理NLP主要模型发展脉络
TS - type
sqlserver删除重复数据
做项目管理有且有必要了解并学习的重要知识--PMP项目管理
postgre 支持 newsql 特性可行性有多大?
Programmer Qixi Gift - How to quickly build an exclusive chat room for your girlfriend in 30 minutes
Diffusion Models:生成扩散模型
密码设置有关方法:不能相同字母,不能为连续字符
"Lonely Walking on the Moon" is a powerful medicine, it can't cure the internal friction of happy twist
MySQL性能指标TPS\QPS\IOPS如何压测?
高手,云集在于REST、gRPC 和 GraphQL之间!
烂大街的缓存穿透、缓存击穿和缓存雪崩,你真的懂了?
c#之winform(软件开发)
跨链桥已成行业最大安全隐患 为什么和怎么办
Motion Regulations (18) - and check the basic questions - gang
面试官:说一下NIO和BIO的区别
Billboard
从零开始配置 vim(7)——自动命令