Java迭代Map方法

Map map=new HashMap();
map.put("1", "one");
map.put("2","two");
map.put("3", "three");
map.put("4", "four");
map.put("5", "five");
map.put("6", "six");

方法一:
Iterator keys = map.keySet().iterator();

while(keys.hasNext()) {
String key = (String) keys.next();
String value=map.get(key);
System.out.println("键"+key+"="+"值"+value);

}

方法二:
Iterator keys = map.entrySet().iterator();

while(keys.hasNext()) {
Map entry =(Map)keys.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
System.out.println("键"+key+"="+"值"+value);

}
方法三:
map.forEach((key, value) -> System.out.println(key + ":" + value));

posted @ 2019-11-09 21:00  拓仲  阅读(2140)  评论(0编辑  收藏  举报