Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Collections.addAll supposed to be faster than c.addAll

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.

So if I understand correctly, a) is slower than b):

a)

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

b)

Collection<Integer> col = new ArrayList<Integer>(); // Collections.addAll(col, Arrays.asList(1, 2, 3, 4, 5)); <-- won't compile Collections.addAll(col, 1, 2, 3, 4, 5); 

Can anyone explain to me, why that is?

edited: corrected code example. thx to polygenelubricants

like image 724
dertoni Avatar asked Jul 27 '10 12:07

dertoni


People also ask

Is addAll faster than add?

AddAll is faster than Add Similarly, addAll provides higher operations per second when compared with add . So next time when you are adding something to an array make sure that you pile them and add it using addAll . The addAll is almost twice as fast as the add version.

What does addAll method do?

The addAll() method of java. util. Collections class is used to add all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array.


1 Answers

Let's take a closer look at the two of them:

// a) 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
like image 123
polygenelubricants Avatar answered Oct 17 '22 08:10

polygenelubricants