Java 遍历Map集合的各种姿势

最常用,在键值都需要时使用。

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

在for-each循环中遍历keys或values。

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

使用Iterator遍历

Map map = new HashMap(); 
Iterator> entries = map.entrySet().iterator(); 
while (entries.hasNext()) { 
  Map.Entry entry = entries.next(); 
  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
}
Map map = new HashMap(); 
Iterator entries = map.entrySet().iterator(); 
while (entries.hasNext()) { 
  Map.Entry entry = (Map.Entry) entries.next(); 
  Integer key = (Integer)entry.getKey(); 
  Integer value = (Integer)entry.getValue(); 
  System.out.println("Key = " + key + ", Value = " + value); 
}

通过键找值遍历(效率低)

Map map = new HashMap(); 
for (Integer key : map.keySet()) { 
  Integer value = map.get(key); 
  System.out.println("Key = " + key + ", Value = " + value);

你可能感兴趣的:(Java 遍历Map集合的各种姿势)