Solved

Copying non-primitive data type variable

Posted on 2011-09-29
7
257 Views
Last Modified: 2012-05-12
Hi, I have a HashMap copied from another HashMap, but I want to change the values only in one of them.

For example...
HashMap hm1 = new HashMap();
hm1.put("fruit", "apple");

HashMap hm2 = new HashMap();
hm2 = hm1;
hm1.put("fruit", "orange");
...

Now both hm1 and hm2 has "orange" under fruit key, but I want only hm1 updated. Is a deep copy the only solution?

Thanks in advance.
0
Comment
Question by:shepp_it
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36815606
A deep copy is the safest solution. btw, you example doesn't show a copy (hm1 == hm2)
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36815637
Yes you need to make a real copy to have them different - otherwise you'll have just two pointers pointing to the same data. You need to creae new Hashjtable and then copy all entries. I think clone() is really implenmented for Hashtable - so you may try using this method. Youi can easily check it buy modifying the clone and checking the original
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36815749
clone() is worth trying, but if (unlike in your example) you have complex, non-primitive objects in the Map, then they might not be copied deeply enough
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:stachenov
ID: 36816083
In this case you're lucky because HashMap has a copy constructor just for that. This should work and make your code even simpler:

HashMap hm1 = new HashMap();
hm1.put("fruit", "apple");

HashMap hm2 = new HashMap(hm1);
hm1.put("fruit", "orange");

Open in new window

0
 
LVL 4

Expert Comment

by:stachenov
ID: 36816124
Oh, and there should be "hm2.put" at the last line - I copied it from your code without testing.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36816158
And clone() is also working at least for strings in HashMap

        HashMap mm1 = new HashMap();
        mm1.put("fruit","apple");
        mm1.put("vegetable","potato");

        HashMap mm2 = (HashMap)mm1.clone();

        mm2.put("vegetable","tomato");

        System.out.println("mm1: " + mm1);

         System.out.println("mm2: " + mm2);

Open in new window



mm1: {fruit=apple, vegetable=potato}
mm2: {fruit=apple, vegetable=tomato}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36816565
>>I think clone() is really implenmented for Hashtable - so you may try using this method.

The implications of clone have little to do with the type on which it's called in this case. The important point is the type of objects in the Map. As i mentioned, clone cannot be considered safe
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now