第五题:Map集合的两种遍历方式
public class Test5 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("鸣人", "雏田");
map.put("佐助", "小樱");
map.put("路飞", "女帝");
map.put("索隆", "师姐");
Set<String> set = map.keySet();
for (String key : set) {
String gf = map.get(key);
System.out.println(key + "\t" + gf);
}
Set<Map.Entry<String, String>> set2 = map.entrySet();
for (Map.Entry<String, String> entry : set2) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "--" + value);
}
}
} |