HashMap cloning?

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

I want to clone a HashMap. HashMap includes a clone method. Great.
But according to the Java docs:

clone()
Returns a shallow copy of this HashMap instance: the keys and
values themselves are not cloned.

Now it seems to me that if you do not clone the keys or the values
contained in the HashMap, what you are left with is an EMPTY HashMap,
which doesn't seem terribly useful, since I could just create a new
HashMap and copy the old one into the new one. Alternately, it could
contain references to the same keys and values in the original, in
which case I might as well just have a second reference to the
original. So what does HashMap.clone() actually do? Do I have to
manually copy everything in it to guarantee that I have a new copy with
the same values?

Thanks!
 
T

Thomas Nelson

I want to clone a HashMap. HashMap includes a clone method. Great.
But according to the Java docs:

clone()
Returns a shallow copy of this HashMap instance: the keys and
values themselves are not cloned.

Now it seems to me that if you do not clone the keys or the values
contained in the HashMap, what you are left with is an EMPTY HashMap,
which doesn't seem terribly useful, since I could just create a new
HashMap and copy the old one into the new one. Alternately, it could
contain references to the same keys and values in the original, in
which case I might as well just have a second reference to the
original. So what does HashMap.clone() actually do? Do I have to
manually copy everything in it to guarantee that I have a new copy with
the same values?

Thanks!


Are you saying all of your keys and values are cloneable?
 
K

Kerry Soileau

Try this:

HashMap original;
....
....
HashMap copyOfOriginal=new HashMap(original);
....

Kerry Soileau
 
W

Wendy S

Alternately, it could
contain references to the same keys and values in the original, in
which case I might as well just have a second reference to the
original.

That's what it does, but it's NOT the same as a second reference to the
original.

With the shallow copy, yes, both HashMaps would contain references to the
same objects. But if you add/remove an item to/from one of the HashMaps,
that wouldn't affect the other.

If you had two references to the original HashMap, then the add/remove would
be seen from both references.
 
N

nooneinparticular314159

Thanks, Wendy!

I'm confused though. I proved to myself that what you said was correct
by creating a HashMap, populating it, and then cloning it. But if they
both reference the same objects, shouldn't changes to one affect the
other? Or is it just adding and removing those references that has no
effect, since those objects are still there, but just aren't referenced
by the new HashMap?

Thanks,
Michael
 
W

Wendy S

I'm confused though. I proved to myself that what you said was correct
by creating a HashMap, populating it, and then cloning it. But if they
both reference the same objects, shouldn't changes to one affect the
other?

The only way changes to one HashMap would affect the other would be if you
had two references to the same HashMap.

Having two separate HashMaps that contain references to the same bunch of
other objects is a different situation entirely.

You won't see it with Strings, for example, but if you put several Person
objects in a HashMap, clone it, then retrieve a Person and change his name,
you will see the new name if you retrieve the same Person from the other
HashMap.

Now remove a Person from one of the HashMaps. The other one will still have
that Person in it.

It's easier to understand if you draw a diagram with arrows showing the
references, but my ASCII art skills are not up to the task. :)
 
R

Remi Bastide

I want to clone a HashMap. HashMap includes a clone method. Great.
But according to the Java docs:

clone()
Returns a shallow copy of this HashMap instance: the keys and
values themselves are not cloned.

Now it seems to me that if you do not clone the keys or the values
contained in the HashMap, what you are left with is an EMPTY HashMap,
which doesn't seem terribly useful, since I could just create a new
HashMap and copy the old one into the new one. Alternately, it could
contain references to the same keys and values in the original, in
which case I might as well just have a second reference to the
original. So what does HashMap.clone() actually do? Do I have to
manually copy everything in it to guarantee that I have a new copy with
the same values?

Thanks!

For a simple and generic deep-copy facility (using serialization) see:
http://sys-con.com/itsg/virtualcd/Java/archives/0511/Callahan/
 
T

Thomas G. Marshall

Wendy S coughed up:

....[rip]...
It's easier to understand if you draw a diagram with arrows showing
the references, but my ASCII art skills are not up to the task. :)


Let me try it without the ascii art. How about using pointers (eeeek) just
to keep the references separate.

Let's pretend that we can peer deep inside the JVM and actually see the
memory locations themselves that are the references. I'll put the memory
locations of each object in parentheses.

If we have a hashmap, it might contain 3 K/V pairs:

hashmap1(1000)
keyA(2000)---valueA(2010)
keyB(2020)---valueB(2030)
keyC(2040)---valueC(2050)

If you assign hashmap2 = hashmap1, then you have two references to the same
hashmap object. That means that if you were to peer inside hashmap2, you
are looking at the exact same hashmap object. Note that using this example,
both references point to location 1000:

hashmap2 = hashmap1
-----------------------------------------
hashmap1(1000)
keyA(2000)---valueA(2010)
keyB(2020)---valueB(2030)
keyC(2040)---valueC(2050)

hashmap2(1000)
keyA(2000)---valueA(2010)
keyB(2020)---valueB(2030)
keyC(2040)---valueC(2050)

If you do a shallow clone of hashmap1 instead, you will have two hashmap
objects, but with all the same keys and values:

hashmap2 = (Hashmap)hashmap1.clone();
-----------------------------------------
hashmap1(1000)
keyA(2000)---valueA(2010)
keyB(2020)---valueB(2030)
keyC(2040)---valueC(2050)

hashmap2(3000) <-----note-----
keyA(2000)---valueA(2010)
keyB(2020)---valueB(2030)
keyC(2040)---valueC(2050)

If you were able to do a deep(er) clone (using your own technique) you might
end up two objects, with copies of all the keys and values:

hashmap2 = magicDeepCloneMethod(hashmap1);
-----------------------------------------
hashmap1(1000)
keyA(2000)---valueA(2010)
keyB(2020)---valueB(2030)
keyC(2040)---valueC(2050)

hashmap2(3000)
keyA(4000)---valueA(4010)
keyB(4020)---valueB(4030)
keyC(4040)---valueC(4050)

Hopefully that will clear things up a little.


--
Having a dog that is a purebred does not qualify it for breeding. Dogs
need to have several generations of clearances for various illnesses
before being bred. If you are breeding dogs without taking care as to
the genetic quality of the dog (again, being purebred is *not* enough),
you are what is known as a "backyard breeder" and are part of the
problem. Most of the congenital problems of present day dogs are
traceable directly to backyard breeding. Spay or neuter your pet
responsibly, and don't just think that you're somehow the exception and
can breed a dog without taking the care described.
 
Joined
Oct 30, 2008
Messages
1
Reaction score
0
try out this example....

import java.util.HashMap;

public class Example {
public static void main(String[] args) {

HashMap<String, String> orgMap = new HashMap<String, String>();

orgMap.put("2000", "2010");
orgMap.put("2020", "2030");
orgMap.put("2040", "2050");

HashMap<String, String> refMap = orgMap;

HashMap<String, String> cloneMap = (HashMap<String, String>) orgMap
.clone();

orgMap.remove("2020");

System.out.println(" Key : 2000 " + orgMap.get("2000")
+ " Key : 2020 " + orgMap.get("2020") + " Key : 2040 "
+ orgMap.get("2040"));

System.out.println(" Key : 2000 " + refMap.get("2000")
+ " Key : 2020 " + refMap.get("2020") + " Key : 2040 "
+ refMap.get("2040"));

System.out.println(" Key : 2000 " + cloneMap.get("2000")
+ " Key : 2020 " + cloneMap.get("2020") + " Key : 2040 "
+ cloneMap.get("2040"));
}


From---- Ramu Kondapalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top