Collections.addAll 和collection.a

作者: IT_Matters | 来源:发表于2016-06-12 21:20 被阅读282次

o>from: http://stackoverflow.com/a/3343829/5032462

在stackoverflow上看到的一篇回答,老外真是太professional了,mark一下,我稍微改了改,加了点自己的批注,最后的summary推荐大家看一下。

The Java API docs say the following about Collections.addAll

The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations.

速度上来说,Collections.addAll要比collection.addAll更快,举例来说。

//a)
Collection<Integer> col = new ArrayList<Integer>();
col.addAll(Arrays.asList(1, 2, 3, 4, 5));

Here's what happens:

  1. varags + autoboxing creates Integer[]
  2. Arrays.asList creates a List<Integer> backed by the array
  3. addAll iterates over a Collection<Integer> using Iterator<Integer>
// b)
Collections.addAll(col, 1, 2, 3, 4, 5);

Here's what happens:

  1. varargs + autoboxing creates Integer[]
  2. addAll iterates over an array (instead of an Iterable<Integer>)

We can see now that b) may be faster because: Arrays.asList call is skipped, i.e. no intermediary List is created. Since the elements are given in an array (thanks to varargs mechanism), iterating over them may be faster than using Iterator.

__
That said, unless profiling shows otherwise, the difference isn't likely to be "significant". Do not optimize prematurely. While Java Collection Framework classes may be slower than arrays, they perform more than adequately for most applications.
__

API links


Collections.addAll(Collection<? super T> c,T... elements)
-varargs i.e. array-based
Collection.addAll(Collection<? extends E> c)
- Collection-based

See also


Java Language Guide/Autoboxing
Java Language Guide/Varargs
Effective Java 2nd Edition, Item 25: Prefer lists to arrays

Related questions
Array or List in Java. Which is faster ?


Summary

  • If you're adding elements from an array, you can use Collections.addAll(col, arr)
    Remember that varargs are also done using arrays

  • If you're adding elements from a Collection, use col.addAll(otherCol)
    Do NOT e.g. Collections.addAll(col, otherCol.toArray())
    Such roundabout way is likely to be slower!

  • It's not that one is supremely faster than the other. It's about skipping unnecessary steps given the current situation

相关文章

  • Collections.addAll 和collection.a

    o>from: http://stackoverflow.com/a/3343829/5032462 在stack...

  • 集合和数组的转换

    集合转数组 数组转集合 (Arrays.asList和Collections.addAll两种方法)

  • List和数组相互转换

    数组转List: Collections.addAll() 笨办法,循环添加数组元素 Arrays.asList(...

  • 第11章 持有对象

    set不重复: Arrays.asList()的用法,1可变参数、2数组Collections.addAll的用法...

  • -和 和 -

    产品介绍:和和是一款会员制共享平台;所有 经营者可在APP内注册和和商家成为会员供 应商(实体店、网店、微商、平台...

  • &和&&,|和||

    原文:https://blog.csdn.net/chinabestchina/article/details/7...

  • 和可和,非常和

    我年纪很小的时候,父亲有一本笔记本,上面只写了一句话:万物并育而不相害,道并行而不相悖。我当时很喜欢这句话,所以期...

  • kotlin中的空? 和 ?. 和 ?: 和 as? 和 !!

    ? 可空类型 kotlin和Java的类型系统之间的一个很重要的区别就是,Kotlin对可空类型的显示支持 也就是...

  • self. 和 _ 和 = 和 set

    声明了一个属性 @property (a,b) p1; 只有用self.调用时修饰关键词才起作用, 用_调用...

  • Observable和Observe和Subcriblers 和

    Observable事件源,被观察者。Subcriblers 观察者,事件订阅者Observer 同Subcrib...

网友评论

    本文标题:Collections.addAll 和collection.a

    本文链接:https://www.haomeiwen.com/subject/gazedttx.html