关于Map集合的遍历总结
2015-07-22 11:50
375 查看
转:http://www.cnblogs.com/lsgspace/p/4666020.html
以下是遍历Map集合的几种方式:
1、通过Map.keySet遍历key和value
2、通过Map.values遍历value
3、通过Map.entrySet和迭代器遍历Map
4、Map.entrySet加for in 循环(推荐)
以下是遍历Map集合的几种方式:
public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("1", "张三"); map.put("2", "李四"); map.put("3", "王五"); //调用 }
1、通过Map.keySet遍历key和value
private static void findKey(Map<String, String> map) { System.out.println("findKey"); for (String key : map.keySet()) { System.out.println("key = " + key); System.out.println("value = " + map.get(key)); } }
2、通过Map.values遍历value
private static void findValue(Map<String, String> map) { System.out.println("findValue"); for (String value : map.values()) { System.out.println("value = " + value); } }
3、通过Map.entrySet和迭代器遍历Map
private static void findIterator(Map<String, String> map) { System.out.println("findIterator"); Iterator<Map.Entry<String, String>> carIterator = map.entrySet() .iterator(); while (carIterator.hasNext()) { Map.Entry<String, String> entry = carIterator.next(); System.out.println("key = " + entry.getKey()); System.out.println("value = " + entry.getValue()); } }
4、Map.entrySet加for in 循环(推荐)
private static void findEntrySet(Map<String, String> map) { System.out.println("findEntrySet"); for (Entry<String, String> entry : map.entrySet()) { System.out.println("key = " + entry.getKey()); System.out.println("value = " + entry.getValue()); } }
相关文章推荐
- 响应式布局小结
- 设计模式之概述
- PHP设计模式漫谈之调解者模式
- POJ 3253 Fence Repair
- CDockablePane使用心得
- Win7+VirtualBox+Mac OS
- Android Context简介
- foreach~~~java中的冒号
- 浅析jQuery对select操作小结(遍历option,操作option)
- android中延迟执行某个任务
- 全屏反锯齿 - 多重采样Ⅰ
- iOS block种类和切换
- 四张图表让你搞懂电离辐射及常用的量
- python threading
- Oracle常用命令
- uva 196 spreadsheet
- IOS中使得图片模糊的效果的方法
- OpenGL中的多重采样
- 运算符和函数
- 001--jcs入门