可能的複製:
2001年12月31日終了的兩年期收入和支出及準備金和基金結餘變動報表 如何有效迭代’Map’中的每個條目?
在HashMap
中迭代專案的最佳方法是什麼?
迭代 entrySet()
,如下所示:
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}
閱讀更多關於 Map
.