通过Map的entrySet方法。将返回一个set集合。然后遍历这个set集合:
package com.howlaa.day04;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class GenericTest {
public static void main(String[] args) {
HashMap<String,Integer> maps = new HashMap<String,Integer>();
maps.put("zhang", 20);
maps.put("hui", 22);
Set<Map.Entry<String,Integer>> entrySet = maps.entrySet();
for (Map.Entry<String, Integer> entry : entrySet) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
本文介绍如何通过Map的entrySet方法获取键值对集合,并使用for-each循环遍历这些集合,展示如何操作Map数据结构。
315

被折叠的 条评论
为什么被折叠?



