迭代HashMap [英] Iterate through a HashMap

查看:149
本文介绍了迭代HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

如何有效地迭代在'Map'中的每个条目?

迭代的最佳方式是什么在 HashMap

What's the best way to iterate over the items in a HashMap?

推荐答案

中的项目上遍历 entrySet() 像这样:

Iterate through the entrySet() like so:

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

Read more about Map.

这篇关于迭代HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆