当前位置:网站首页>The four traversal methods of the map set can be seen at a glance
The four traversal methods of the map set can be seen at a glance
2022-06-29 03:25:00 【Su bro】
Traverse Map And traversal List There's a big difference , Beginners have never touched map You may not understand the code , I'd like to make a brief statement here .
Map The element stored in is in the form of “ Key value pair ” The way , That is to say key-value Yes .
Map.Entry yes Map One of the interfaces in , Its purpose is to represent a mapping item ( There are Key and Value), As shown in the figure below :
So we can traverse through Map Of entrySet() Method , It returns an implementation Map.Entry Object collection of interface , Every entry Object to store the pair K-V Yes . We can call... Through this object getKey() and getValue() Get what you need easily key and value
The second and third traversal methods in the following code are used entrySet() Method
//Map Four ways to traverse a set
public class MapDemo {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("tom",11);
map.put("cindy",22);
map.put("marry",42);
//1、 The first traversal method , obtain key--> to value
for(String key : map.keySet()){
//keySet() Returns the... Of the key contained in this map Set View
System.out.print(key+"="+ map.get(key)+" ");
}
//2、 The second traversal method , adopt Map.entrySet Use iterator Traverse key and value
Set<Map.Entry<String, Integer>> entries = map.entrySet(); //entrySet() Returns the... Contained in this map entry Of Set View
Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<String, Integer> next = iterator.next();
System.out.print(next.getKey()+"="+next.getValue()+" ");
}
//3、 The third way of traversal , adopt Map.entrySet Traverse key and value
for (Map.Entry<String,Integer> entry : map.entrySet()){
System.out.print(entry.getKey()+"="+entry.getValue()+" ");
}
//4、 The fourth way of traversal , adopt Map.values() Traverse all value, But you can't traverse key
for (Integer integer: map.values()){
System.out.print("value="+integer+" ");
}
}
}
边栏推荐
- 嵌入式开发如何进行源代码保密工作
- Zigzag sequence traversal of binary tree [one of layered traversal methods - > preorder traversal +level]
- Etcd tutorial - Chapter 7 etcd transaction API
- Digital twin application of smart Park Based on Web GIS aerial photography
- Grafana入门教程
- 蓝桥杯2022初赛——扫雷
- 2022/02/15
- vim配置与使用
- 相同的树[从部分到整体]
- 测试入门——集成测试
猜你喜欢

2022-2028 global long wave infrared camera and camera core industry research and trend analysis report

How does kubernetes store business data persistently? (10)

设备监理师证书含金量怎样?值得考吗?

Basic MySQL database operations

How to keep source code secret in embedded development
![[issue 259] uncover how count is executed in MySQL?](/img/bc/ee26eba95f408ea887d6aa06301a0b.jpg)
[issue 259] uncover how count is executed in MySQL?

如何理解MySQL的索引?

Nvisual helps integrators transform

leetcode:560. Subarray with and K

vim配置与使用
随机推荐
map,set用pari作为key值,如何定义
Jerry's watch obtains alarm mode settings [chapter]
Certification training | streamnational certification training phase 2
相同的树[从部分到整体]
Data discretization
不同的二叉搜索树[自下而上回溯生成树+记忆搜索--空间换时间]
数据离散化
Synchronous real-time data of Jerry's watch [chapter]
Is it safe for qiniu school to open an account in 2022?
2022-2028 global low carbon concrete industry research and trend analysis report
If you dare to write MQ message queue middleware on your resume, you must win these questions!
Etcd教程 — 第六章 Etcd之核心API V3
What is the gold content of the equipment supervisor certificate? Is it worth it?
Démarrer le test - test d'intégration
Gartner“客户之声”最高分,用户体验成中国数据库一大突破口
归并排序
2022/02/15
[leetcode daily question] number of schemes to reconstruct a tree
Jerry's watch begins to move [chapter]
Ten commands commonly used in SVN