遍历map的四种方式
来源:互联网 发布:文件加密解密软件 编辑:程序博客网 时间:2024/06/05 05:53
本文转载自http://www.cnblogs.com/kristain/articles/2033566.html
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的四种方式
- cocos2d-JS 监听屏幕大小改变函数
- Unreal 获取屏幕宽度和高度
- 初始ZooKeeper与集群搭建实例
- EXOplayer简书
- linux下.run文件的安装与卸载
- 遍历map的四种方式
- 云计算平台项目团队组织架构与缘起
- Miscellaneous | 判断两个字符串是变位词
- contiki学习地址
- 微博转载-是时候了,也该做个了结了。
- Leetcode 33 Search in Rotated Sorted Array 二分查找变式
- android Studio ddms中emulator control不能用的问题
- 处理mysql返回ztree数据格式问题
- Retrofit2.0入门讲解