Java如何遍历Map对象的4种方法 今天比昨天,更优秀 Map简介 Map是用于保存具有映射关系的数据,保存两组值,key和value,这两组值可以是任何应用类型的数据。Map典型的实现类是H
今天比昨天,更优秀
Map<String, String> map = new HashMap<String, String>(); for (Map.Entry <String, String> entry : map.entrySet()) { System.out.println("Key = " + entry.geTKEy() + ", Value = " + entry.getValue()); }
注意:如果Map值是空的则会报错,抛出NullPointerException异常。
Map<String, String> map = new HashMap<String, String>(); //遍历map中的键for (String key : map.keySet()) { System.out.println("Key = " + key);}//遍历map中的值for (Integer value : map.values()) { System.out.println("Value = " + value);}
Map<String, String> map = new HashMap<String, String>();Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();while (entries.hasNext()) { Map.Entry<String, String> entry = entries.next(); System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());}
Map<String, String> map = new HashMap<String, String>();for (String key : map.keySet()) { Integer value = map.get(key); System.out.println("Key = " + key + ", Value = " + value);}
Map<String, String> map = new HashMap<String, String>();map.forEach((key, value) -> { System.out.println("Key = " + key + ", Value = " + value);});
Map<String, String> map = new HashMap<String, String>();for (int i = 0; i < map.size(); i++) { String key = keySet.get(i); String Value = map.get(key); System.out.println("Key = " + key + ", Value = " + value); }
大家可以根据自己需要,自由选择。
来源地址:https://blog.csdn.net/laterstage/article/details/131774885
--结束END--
本文标题: Java如何遍历Map对象集合(多种方式)6种方式
本文链接: https://lsjlt.com/news/425301.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0