当前位置:网站首页>Map集合的四种遍历
Map集合的四种遍历
2022-06-23 21:56:00 【Liquor...】
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "ab");
map.put(4, "ab");
map.put(4, "ab");// 和上面相同 , 会自己筛选
System.out.println(map.size());
// 第一种:
/* * Set<Integer> set = map.keySet(); //得到所有key的集合 * * for (Integer in : set) { String str = map.get(in); * System.out.println(in + " " + str); } */
System.out.println("第一种:通过Map.keySet遍历key和value:");
for (Integer in : map.keySet()) {
//map.keySet()返回的是所有key的值
String str = map.get(in);//得到每个key多对用value的值
System.out.println(in + " " + str);
}
// 第二种:
System.out.println("第二种:通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
// 第三种:推荐,尤其是容量大时
System.out.println("第三种:通过Map.entrySet遍历key和value");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
//Map.entry<Integer,String> 映射项(键-值对) 有几个方法:用上面的名字entry
//entry.getKey() ;entry.getValue(); entry.setValue();
//map.entrySet() 返回此映射中包含的映射关系的 Set视图。
System.out.println("key= " + entry.getKey() + " and value= "
+ entry.getValue());
}
// 第四种:
System.out.println("第四种:通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values()) {
System.out.println("value= " + v);
}
}
边栏推荐
- How to set the website construction title bar drop-down
- 专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!
- PHP时间戳
- How to build a business analysis system
- sql server常用sql
- Ranking of high cost performance commercial endowment insurance products in 2022
- How to shut down the server in the fortress machine? What other operations can the fortress machine perform?
- Debian change source and uninstall useless services
- Problems and solutions of MacOS installation go SQLite3
- Application practice | Apache Doris integrates iceberg + Flink CDC to build a real-time federated query and analysis architecture integrating lake and warehouse
猜你喜欢

蚂蚁集团自研TEE技术通过国家级金融科技产品认证

Ant group's self-developed tee technology has passed the national financial technology product certification

C#/VB. Net word to text
Trigger definition and syntax introduction in MySQL

Save: software analysis, verification and test platform

混沌工程,了解一下
Detailed usage of exists in SQL statements

SAVE: 软件分析验证和测试平台

In the eyes of the universe, how to correctly care about counting East and West?

The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!
随机推荐
How to set up links for website construction how to build a website
国家邮政局等三部门:加强涉邮政快递个人信息安全治理,推行隐私面单、虚拟号码等个人信息去标识化技术
Tcapulusdb Jun · industry news collection
December 14, 2021: rebuild the queue according to height. Suppose there's a bunch of people out of order
Three ways to enable IPv6 on Tencent cloud
How to set the search bar of website construction and what should be paid attention to when designing the search box
JMeter pressure measuring tool beginner level chapter
C#/VB.NET Word转Text
FTP server setup setting website information can I set up FTP myself
Mysql中的触发器定义及语法介绍
How to use xshell to log in to the server through the fortress machine? How does the fortress machine configure the tunnel?
Batch production of plant hangtag
How to set secondary title in website construction what is the function of building a website
How does data Vientiane CI | app quickly integrate HLS encryption to prevent video leakage?
How PostgreSQL creates partition tables
Website construction column setting form which website construction company is better
【技术干货】蚂蚁办公零信任的技术建设路线与特点
Virtual machine performance monitoring and fault handling commands on the console
How to judge the video frame type in h265 in golang development
Ant group's self-developed tee technology has passed the national financial technology product certification