发布日期:2018-03-26
遍历HashMap+ 查看更多
遍历HashMap
+ 查看更多
发布日期:2018-03-10 14:27
分类:JAVA
浏览次数:47
遍历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 } }