Map遍历方法
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("-------------------");
}