如何有效地遍历Java Map中的每个条目? [英] How do I efficiently iterate over each entry in a Java Map?

查看:74
本文介绍了如何有效地遍历Java Map中的每个条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个用Java实现Map接口的对象,并且希望对其中包含的每一对进行迭代,那么遍历该映射的最有效方法是什么?

If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map?

元素的顺序是否取决于我对接口的特定映射实现?

Will the ordering of elements depend on the specific map implementation that I have for the interface?

推荐答案

Map<String, String> map = ...
for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println(entry.getKey() + "/" + entry.getValue());
}

这篇关于如何有效地遍历Java Map中的每个条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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