Solved

Copying non-primitive data type variable

Posted on 2011-09-29
7
266 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

770 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