HashMap的迭代方法
HashMap的迭代方法
如果你只对Key感兴趣,你可以用keySet()
Map<String, Object> map = ...;for (String key : map.keySet()) { // ...}
for (Object value : map.values()) { // ...}
for (Map.Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); // ...}
public static void printMap(Map mp) { Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry)it.next(); System.out.println(pairs.getKey() + " = " + pairs.getValue()); it.remove(); // avoids a ConcurrentModificationException }}