遍历map

遍历map的常用方式一:键值都要

Map map = new HashMap(); 
for (Map.Entry entry : map.entrySet()) { 
  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
}

遍历map的常用方式二:只要键或值

Map map = new HashMap(); 
//遍历map中的键 
for (Integer key : map.keySet()) { 
  System.out.println("Key = " + key); 
} 
//遍历map中的值 
for (Integer value : map.values()) { 
  System.out.println("Value = " + value); 
}

 

 

你可能感兴趣的:(遍历map)