• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deep Copy of HashMap  RSS feed

 
Sandeep Kumar Jakkaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All

How can I make a deep copy of a java.util.HashMap ?

Thanks
Sandeep Kumar Jakkaraju
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandeep Kumar Jakkaraju wrote:Hello All

How can I make a deep copy of a java.util.HashMap ?

Thanks
Sandeep Kumar Jakkaraju


is deep copy = cloning of object?
 
Sandeep Kumar Jakkaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I want

HashMap original = new HashMap();

HashMap deepCopy = deepCopy(original);

so that if I change the original ... my deepCopy wont change !!!

I know that orginal.clone() would return a shallow copy !!!

I hope I am right !!!
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandeep Kumar Jakkaraju wrote:Here is what I want

HashMap original = new HashMap();

HashMap deepCopy = deepCopy(original);

so that if I change the original ... my deepCopy wont change !!!

I know that orginal.clone() would return a shallow copy !!!

I hope I am right !!!



here, you crated deepCopy is a different object in case you'll change the 'original'
and clone method also does same thing as you'll have to store clonned object into another object ie you'll have to assign to a different object name

so i am not getting what you finding for...sorry

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A deep copy is merely done by iterating through the elements (keys and values) and cloning those too.

So, instead of cloning the hashmap, you create a new hashmap, iterate through the pairs, cloning the keys and values, and placing the clone values into the new map.

Of course, for this to work, the elements have to be clonable.


[BTW, this topic was originally in JiG:Advanced. I moved it here, but wasn't actually sure if this was beginners or intermediate -- the the final decision was done with a coin flip... sorry... ]

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so that if I change the original ... my deepCopy wont change !!!

I know that orginal.clone() would return a shallow copy !!!


With a shallow copy, if you change the structure of the hashmap (by adding new or deleting entries), your copy won't change either. Your copy will only change, if your elements are mutable, and you change the values in the elements.

Henry
 
Sandeep Kumar Jakkaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sir Henry

I have this problem

HashMap m = new HashMap();

m.put("myObj","0");

while(somthing)
{

Thread t = new MyThread(m);
t.start();
}


Now each MyThread changes the "myObj" ....... like m.put("myObjt","x");

This will lead to different results everytime .... due context switching when the race condition occurs ....

I hope this is in JIG:Advanced

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now each MyThread changes the "myObj" ....... like m.put("myObjt","x");

This will lead to different results everytime .... due context switching when the race condition occurs ....



Nope. Strings are immutable. The shallow copy provided by the hashmap clone() method should be fine. There is no need to clone the elements.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!