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

  • SEARCH
  • Home
  • 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 7816335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:49:55+00:00 2026-06-02T05:49:55+00:00

While inspecting ArrayList API, I noticed something that looks strange. Indeed, here the the

  • 0

While inspecting ArrayList API, I noticed something that looks strange.

Indeed, here the the ArrayList constructor implementation with a Collection passed as argument :

public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        size = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
}

and here the equivalent into the HashSet Class :

public HashSet(Collection<? extends E> c) {
        map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
        addAll(c);
}

So, we can notice that the ArrayList one used a COPY (Arrays.copyOf) of elements provided by the collection in parameter whereas the HashSet one, use addAll() method.

And of course, addAll() method doesn’t copy elements but just add references to the HashSet collection.

I find this subtil difference “dangerous” for caller that ignores it.

One could expect a collection with SAME references, another one who read ArrayList API well would expect a COPY of elements from original Collection.

Why didn’t Sun provide same concept for those Collections subclasses ?

  • 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-06-02T05:49:57+00:00Added an answer on June 2, 2026 at 5:49 am

    ArrayList one used a COPY (Arrays.copyOf) of elements provided by the collection in parameter whereas the HashSet one, use addAll() method.

    No, Arrays.copyOf only copies the array, but not objects this array points to. The objects aren’t cloned. That being said both constructors behave the same – they will contain references to the same objects as the original collection. Modifying an object in one collection will modify it in the other (because it is the same object).

    Also note that Arrays.copyOf() is only used in some circumstances.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While inspecting delegates in C# and .NET in general, I noticed some interesting facts:
I'm debugging a project that uses precompiled headers under GDB. While inspecting crash stack
While inserting rows into SQL Server CE in WP7 I'm getting a SQLCeException that
While inspecting an element using Chrome's devtools, in the elements tab, the right-hand side
While i was inspecting facebook html code through with firebug in Chrome, i found
While downloading Google Guice I noticed two main types of artifacts available on their
I'm inspecting an open source web application. There's aproblem that it used error_get_last function
While inspecting the the .net object model in the Object Browser window, I came
I am calling a vendor's Java API, and on some servers it appears that
Getting error while inserting values into database (SQL Server 2008) Implicit conversion from data

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.