Map<String, String> 遍历的四种方法
2015-10-27 17:47
543 查看
Map<String, String> map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "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()); } <span style="color:#FF0000;"> //第三种:推荐,尤其是容量大时</span> 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); }
相关文章推荐
- jQuery 中 attr() 和 prop() 方法的区别
- Problem creating zip: Execution exce ption (and the archive is probably corrupt but I could not delete it): Java heap space -> [Help 1]
- MediaRecorder录像那些事
- system 函数
- 运用冒泡排序法-从小到大排列数组中的元素
- iOS tableView 自带的下拉刷新
- 信息安全系统设计基础第七周学习总结
- mysql中select中in()查询语句 原样输出
- C可变参数函数实现
- 合并两有序数组 时间O(n) 无辅助空间
- git reset小结
- python 装饰器
- Unity3D中的socket通信之九(账号密码/数据传输过程中的加密解密)
- 反射机制----Class
- Android开发,仿苹果的弹出窗口界面、以及窗口动画效果、窗口圆角实现。
- Sql查询的坑
- Web服务器集群搭建
- Undefined symbols for architecture i386: "_OBJC_CLASS_$_AVOSCloud", referenced from:
- C#学习(6):DataSet构建EasyUI树结构
- startssl,免费的ssl证书申请及注意事项