遍历map的四种方式
来源:互联网 发布:伦纳德死亡缠绕 知乎 编辑:程序博客网 时间:2024/06/06 12:28
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
//第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (String key : map.keySet()) {
System.out.println("key= "+ key + " and value= " + map.get(key));
}
//第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第三种:推荐,尤其是容量大时
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<String, String> entry : map.entrySet()) {
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);
}
}
- map的四种遍历方式,map遍历元素
- 四种Map集合的遍历方式
- 四种遍历 map 的方式
- 遍历Map的四种方式
- map的四种遍历方式
- 遍历Map的四种方式
- 遍历Map集合的四种方式
- Java Map的四种遍历方式
- map遍历的四种方式
- 遍历 Map 的四种方式
- map的四种遍历方式
- 遍历Map的四种方式
- 遍历map的四种方式
- map的四种遍历方式
- 遍历map的四种方式
- 遍历map的四种方式
- Map的四种遍历方式
- 遍历Map的四种方式
- stm32f407 GPIO_PinAFConfig 函数
- 这些渐行渐远的友情,你有过吗?
- 日期格式化
- 使用倒序索引提升ORDER BY DESC性能
- FreeRTOS高级篇3---FreeRTOS调度器启动过程分析
- 遍历map的四种方式
- 计算机概述
- 161018
- mysql索引总结----mysql 索引类型以及创建
- 文章标题
- LoRa开发8: 下载源代码
- 黑马程序员:Java学习路线大揭秘连载2-JavaWeb篇!
- https握手时交互的内容
- 雅虎十四条:网站前端网页优化的14条原则