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!
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!