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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:20:22+00:00 2026-05-15T00:20:22+00:00

I normally do this to set a new Map to a private variable: public

  • 0

I normally do this to set a new Map to a private variable:

public static void setListaClausulas(Map<String, Clausula> nvLista) {
    listaClausulas = new TreeMap<String, Clausula>(nvLista);
}

I suppose this is ok to set a new copy of the nvLista and all it’s members and not a reference, is it?

But now I have a Map inside another Map and I’m doing this:

public static void setListaClausulas(Map<String, Map<String, Clausula>> nvLista) {
    listaClausulas = new TreeMap<String, Map<String, Clausula>>(nvLista);
}

Is this the correct way to do it or do you recommend something else? What I want is to set a new copy of nvLista (and all it’s elements) and not copy just the reference.

  • 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-15T00:20:23+00:00Added an answer on May 15, 2026 at 12:20 am

    I guess you are worried about the maps being passed in your method parameter will be mutated?

    You need to create a deep-copy of the parameter. Various approaches are discussed in this SO question, deep-clone-utility-recomendation

    EDIT: In response to comment, here’s a coded version. This doesn’t deep copy the Clausula instances, since they were not copied before – I’m assuming they’re immutable.

    public Map<String, Map<String, Clausula>> deepCopy(Map<String, Map<String, Clausula>> nvLista)
    {
       Map<String, Map<String, Clausula>> target = new TreeMap<String, Map<String, Clausula>>();
       for (String key: nvLista.keySet()) {
           Map<String, Clausula> value = nvLista.get(key);
           target.put(key, new TreeMap<String,Clausula>(value));
       }
       return target;
    }
    

    However, nesting collection types like this quickly becomes unreadable. If you can change the code, it may help readability to create a wrapper object for the innermost map.

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

Sidebar

Related Questions

I normally save new data to the database like this: $this->MyTable->set(array( 'id' => $id,
Normally when I want to query large result set using Mysql I write this
Without the database part you normally create a new object in Javascript like this:
my jQuery skills are pretty good normally but this is driving me mad! It's
I am doing some tests, just to learn (normally i do this in ajax
Normally I would just do this. $str = preg_replace('#(\d+)#', ' $1 ', $str); If
I normally do most of this work in Excel 2007, but I do not
Normally we have to do like this to invoke a function from a function
Normally the XML files I have to parse are like this: <row id=1> <title>widget<title>
This kind of code would normally work in PHP, but since the scope 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.