HashMap<String, String> nameMap = new HashMap<>();
nameMap.put("貂蝉", "婵婵");
nameMap.put("吕布", "奉先");
nameMap.put("曹操", "孟德");
nameMap.put("董卓", "仲颖");
//第一种使用Entry
Set<Entry<String, String>> eSet = nameMap.entrySet();
for (Entry<String, String> et : eSet) {
System.out.println("名是: " + et.getKey() + ", 字是:" + et.getValue());
}
//第二种使用Set
Set<String> keySet = nameMap.keySet();
for (String key : keySet) {
System.out.println("名为:" + key + ", 字为: " + nameMap.get(key));
} |
|