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 7764725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:59:17+00:00 2026-06-01T14:59:17+00:00

I have a general question with regards to immutable collections. I will use Java

  • 0

I have a general question with regards to immutable collections. I will use Java as a reference labguage as I know it best.

Firstly, is there a general approach to assuring immutability? By that I mean whether one should copy the entire collection, make changes and then return the new object? Is there any more complex, general approach?

Taking this a bit further, what about “obvious” (to me) mutable collections, such as trees? Usually they are implemented as Nodes with N children nodes. How would you assure immutability in this case? Clone and Copy all references recursively?

Following from the above, I was wondering whether what would be best approach to these collections:

  1. List – array-based which we would copy before making changes?
  2. Queues – two arrays for front and back, cloned before returning? What about bode based implementation?
  3. Trees
  4. Hashmap – copy the bucket array, as well as all collision lists?
  5. Set

Thank you very much for your responses.

  • 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-01T14:59:18+00:00Added an answer on June 1, 2026 at 2:59 pm

    Happily, much of this has already been done for you, check out google guava which has ImmutableCollection, ImmutableList, ImmutableSet, ImmutableMap etc etc.

    Immutability is ensured by preventing subclasses of these classes (by either making them final, or making their constructors private).

    When you make an immutable collection from a mutable one, the data in the mutable collection is copied, then all mutation operations are disallowed – they will throw an exception (UnsupportedOperationException for example).

    For performance reasons, the guava libraries will do their best not to copy data if they don’t need to. For example, if you already have an ImmutableMap, and create a new one with ImmutableMap.copyOf(theOtherImmutableMap), then no data is copied, since we already know the other map is immutable, it’s ok to have two references to the same data.

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

Sidebar

Related Questions

I have a very general question regarding the use of LINQ vs SQL to
I have general question regarding the use of pointers vs. references in this particular
I have general question about Linux. Will the inode be created if I create
I have a general question: is it possible in Java to read in a
I have a general question regarding memory management of properties. Currently, I always use
I have a general question that is rather open-ended (i.e. depends on platform, application
I have a general question for the group. I am about to start a
I have a general question about the way that database indexing works, particularly in
I have a general question. I would like to have a window containing some
i have a general question about how sql server evaluates the joins.The query is

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.