/*要求:两种方式遍历Map集合*/
public class setmap {
public static void main(String[] args) {
HashMap<String , String> map= new HashMap<String, String>();
map.put("邓超", "孙俪");
map.put("黄晓明", "杨颖");
map.put("沙溢", "胡可");
//方法一
Set<String> keySet = map.keySet();
for (String str: keySet) {
String value = map.get(str);
System.out.println(str+"媳妇是"+value);
}
System.out.println("======================");
//方法二
Set<Entry<String, String>> entrySet = map.entrySet();
for (Entry<String, String> s : entrySet) {
String key = s.getKey();
String value = s.getValue();
System.out.println(key+"媳妇是"+value);
}
}
} |
|