回答

收藏

遍历 HashMap

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

迭代 a 项目的最佳方法是HashMap什么?
) n0 U* }+ W( a- Q7 ]8 x4 X2 j) y; m                                                                ! y7 p# S5 d' s
    解决方案:                                                               
; v4 {6 T; f+ r                                                                entrySet()像这样迭代:3 O! ~# H! S5 E/ f2 W2 K+ a
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
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则