下面两种方式都可以获取Map中的键值对:
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("001", "a");
map.put("002", "b");
map.put("003", "c");
map.put("004", "d");
Set<String> set = map.keySet(); //方式一
Set<Map.Entry<String, String>> entrySet = map.entrySet(); //方式二
for (Iterator<String> it = set.iterator(); it.hasNext();) {
String str = it.next();
System.out.println("key:" + str + " value:" + map.get(str));
}
System.out.println("============美丽的分割线===========");
for (Iterator<Map.Entry<String, String>> it = entrySet.iterator(); it
.hasNext();) {
Map.Entry<String, String> mapEntry = it.next();
System.out.println("key=" + mapEntry.getKey() + " value="
+ mapEntry.getValue());
}
} |