回答

收藏

遍历 HashMap

技术问答 技术问答 361 人阅读 | 0 人回复 | 2023-09-11

迭代 a 项目的最佳方法是HashMap什么?
( J0 X+ z$ s" d" L                                                                * Q/ a- |% H1 x5 q, P8 y
    解决方案:                                                                6 e7 W+ C# V! e7 p9 ?1 C) N# s
                                                                entrySet()像这样迭代:  q$ ?! h* b; A3 U( w- f
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
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则