Solved

Copying non-primitive data type variable

Posted on 2011-09-29
7
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

710 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