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));

你可能感兴趣的:(java)