JAVA:设置 addAll() 与使用循环添加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24203950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:48:15  来源:igfitidea点击:

JAVA : Set addAll() vs adding using loop

javaperformance

提问by Niche

I was trying to add Integers from 2 sets into single Set via for loop and also using addAll() method provided by Collections. For test purpose, I have populated 2 Sets with Integers and then tried adding them to third set

我试图通过 for 循环将 2 个集合中的整数添加到单个 Set 中,并使用 Collections 提供的 addAll() 方法。出于测试目的,我用整数填充了 2 个集合,然后尝试将它们添加到第三个集合

        Set<Integer>  undId = new HashSet<Integer>();
        Set<Integer>  proxies = new HashSet<Integer>();
        //Create 2 sets with Integers
        for(int i=0;i<100;i++){
            undId.add(i);
            proxies.add(i);         
        }

and method 1 : //Now add them to third set using for loop

和方法1://现在使用for循环将它们添加到第三组

            for(Integer integer : undId)
            underlyings.add(integer);

        for(Integer integer :proxies)
            underlyings.add(integer);

and method 2 ://Or add them to third set using addAll()

和方法 2 ://或使用 addAll() 将它们添加到第三组

            underlyings.addAll(undId);
        underlyings.addAll(proxies);    

Now when i was trying to time the operation using System.nanoTime(), add is twice faster (for 100,1000,10000 elements). When i increased size to 1000000 or 10000000. It was reversed. I was wondering why would it happen for larger set. I am not sure how addAll() internally handles however any help in understanding above will be appreciated. Thnx

现在,当我尝试使用 System.nanoTime() 对操作进行计时时,添加速度要快两倍(对于 100,1000,10000 个元素)。当我将大小增加到 1000000 或 10000000 时,情况正好相反。我想知道为什么更大的集合会发生这种情况。我不确定 addAll() 内部如何处理,但对理解上述内容的任何帮助将不胜感激。谢谢

采纳答案by Tim B

Before you doing anything make sure you've read and understood the discussion here: Java benchmarking - why is the second loop faster?

在您做任何事情之前,请确保您已阅读并理解此处的讨论:Java 基准测试 - 为什么第二个循环更快?

I would expect addAll to be faster in some situations as it has more information to work with.

我希望 addAll 在某些情况下会更快,因为它有更多的信息可供使用。

For example on an ArrayList addAll can make sure it allocates enough space to add every single element in one step, rather than having to reallocate multiple times if adding large numbers of elements.

例如,在 ArrayList 上 addAll 可以确保它分配足够的空间来一步添加每个元素,而不是在添加大量元素时重新分配多次。

It would certainly not be slower as even a naive implementation of it would just do what you do, loop through adding the items.

它肯定不会变慢,因为即使是它的天真实现也会做你所做的,循环添加项目。

回答by Vinay Rao

Check the implementation of the addAll method - nothing different that what you'd do - AbstractCollection code

检查 addAll 方法的实现 - 与您所做的没有什么不同 - AbstractCollection 代码

回答by Alexander Rühl

Concerning your question about how it is handled internally, just check the available source code, e.g. Ctrl-click in eclipse and find the implementation, which in your case is in AbstractCollection:

关于您关于如何在内部处理的问题,只需检查可用的源代码,例如在 Eclipse 中按 Ctrl-click 并找到实现,在您的情况下是AbstractCollection

    public boolean addAll(Collection<? extends E> c) {
      boolean modified = false;
      for (E e : c)
        if (add(e))
            modified = true;
      return modified;
    }

(from JDK 1.7)

(来自 JDK 1.7)