Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 941305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:05:59+00:00 2026-05-15T22:05:59+00:00

The Java API docs say the following about Collections.addAll The behavior of this convenience

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:06:00+00:00Added an answer on May 15, 2026 at 10:06 pm

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am calling a vendor's Java API, and on some servers it appears that
as I googled for problem, somehow java printing API is crippled with limitation that
I just played with Java file system API, and came down with the following
I was reading the Java api docs on Hashtable class and came across several
I am using Bonita Api Java docs(Bonita Api) to get the instanceUUID of process
Java fb API I am creating facebook web application, to create even following is
http://download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlSeeAlso.html According to this, I should be able to make my root element have
The link http://download.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html describes several interesting data structures like concurrent skip-list. Is there any
According to http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#getDeclaredMethods%28%29 , Class.getDeclaredMethods() should only include methods declared by the class. However,
To recap for those .NET gurus who might not know the Java API: ConcurrentHashMap

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.