Solved

Copying non-primitive data type variable

Posted on 2011-09-29
7
246 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 4

Expert Comment

by:stachenov
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

8 Experts available now in Live!

Get 1:1 Help Now