首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java :通过HashMap迭代,哪个更高效?

Java :通过HashMap迭代,哪个更高效?
EN

Stack Overflow用户
提问于 2011-04-29 07:52:05
回答 7查看 79.5K关注 0票数 62

给定以下代码,并使用两种可选的方法对其进行迭代,

这两种方法在性能上有什么区别吗?

代码语言:javascript
复制
        Map<String, Integer> map = new HashMap<String, Integer>();
        //populate map

        //alt. #1
        for (String key : map.keySet())
        {
            Integer value = map.get(key);
            //use key and value
        }

        //alt. #2
        for (Map.Entry<String, Integer> entry : map.entrySet())
        {
            String key = entry.getKey();
            Integer value = entry.getValue();
            //use key and value
        }

我倾向于认为alt. #2是迭代整个map的更有效的方法(但我可能错了)。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2011-04-29 08:24:51

第二个选项肯定更有效率,因为与第一个选项中的n次相比,您只需要查找一次。

但是,没有什么比在你可以的时候尝试更好的了。所以是这样的-

(不是很完美,但足以在我的机器上验证假设)

代码语言:javascript
复制
public static void main(String args[]) {

    Map<String, Integer> map = new HashMap<String, Integer>();
    // populate map

    int mapSize = 500000;
    int strLength = 5;
    for(int i=0;i<mapSize;i++)
        map.put(RandomStringUtils.random(strLength), RandomUtils.nextInt());

    long start = System.currentTimeMillis();
    // alt. #1
    for (String key : map.keySet()) {
        Integer value = map.get(key);
        // use key and value
    }
    System.out.println("Alt #1 took "+(System.currentTimeMillis()-start)+" ms");

    start = System.currentTimeMillis();
    // alt. #2
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        String key = entry.getKey();
        Integer value = entry.getValue();
        // use key and value
    }
    System.out.println("Alt #2 took "+(System.currentTimeMillis()-start)+" ms");
}

RESULTS (一些有趣的)

使用int mapSize = 5000; int strLength = 5;

Alt #1耗时26毫秒

Alt #2耗时20毫秒

使用int mapSize = 50000; int strLength = 5;

Alt #1耗时32毫秒

Alt #2耗时20毫秒

使用int mapSize = 50000; int strLength = 50;

Alt #1耗时22毫秒

Alt #2耗时21毫秒

使用int mapSize = 50000; int strLength = 500;

Alt #1耗时28毫秒

Alt #2耗时23毫秒

使用int mapSize = 500000; int strLength = 5;

Alt #1耗时92毫秒

Alt #2耗时57毫秒

...and等

票数 65
EN

Stack Overflow用户

发布于 2011-04-29 08:00:25

第二个代码片段会稍微快一些,因为它不需要重新查找键。

所有HashMap迭代器都调用nextEntry method,后者返回一个Entry<K,V>

第一个代码片段丢弃条目中的值(在KeyIterator中),然后再次在字典中查找它。

您的第二个代码片段直接使用键和值(来自EntryIterator)

( keySet()entrySet()都是廉价的调用)

票数 10
EN

Stack Overflow用户

发布于 2014-10-02 02:51:39

映射:

Map<String, Integer> map = new HashMap<String, Integer>();

除了两个选项之外,还有一个选项。

1) keySet() -如果您只需要使用 keys,请使用它

代码语言:javascript
复制
for ( String k : map.keySet() ) {
    ...
}

2) entrySet() -如果您需要同时使用它:keys & values

代码语言:javascript
复制
for ( Map.Entry<String, Integer> entry : map.entrySet() ) {
    String k = entry.getKey();
    Integer v = entry.getValue();
    ...
}

3) values() -如果您只需要 values,则可以使用它

代码语言:javascript
复制
for ( Integer v : map.values() ) {
    ...
}
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5826384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档