如何有效地迭代Java Map中的每个条目? [英] How to efficiently iterate over each entry in a Java Map?

查看:102
本文介绍了如何有效地迭代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天全站免登陆