首页 > 其他 > 详细

Map遍历方法

时间:2015-01-08 09:24:04      阅读:308      评论:0      收藏:0      [点我收藏+]

Map遍历只要有两种方法:

1.通过Map的KeySet进行遍历

2.通过Map的EntrySet进行遍历


[java] view plaincopy在CODE上查看代码片派生到我的代码片
// Map的遍历方法一:通过map的KeySet进行遍历
@Test
public void test4() {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "good");
map.put(2, "morning");

Set<Integer> set = map.keySet();
for (Integer ky : set) {
System.out.println(ky + ":" + map.get(ky));
}

System.out.println("-------------------");
}

// Map的遍历方法二:通过map的entrySet进行遍历
@Test
public void test5() {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "good");
map.put(2, "morning");

Set<Map.Entry<Integer, String>> set = map.entrySet();
for (Entry<Integer, String> entry : set) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}

System.out.println("-------------------");
}

Map遍历方法

原文:http://www.cnblogs.com/netcorner/p/4209898.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!