Is there java utility that does clone()
method for HashMap
such that it does copy of the map elements not just the map object (as the clone()
in HashMap
class)?
What about other objects referred to in the elements? How deep do you want your clone?
If your map elements don't have any deep references and/or everything is Serializable
, you can serialize the map via ObjectOutputStream
into a ByteArrayOutputStream
and then deserialize it right away.
The only other alternative is to do it manually.
The SO question Deep clone utility recommendation is similar to this one, and has an answer that may be helpful to you.
To summarize, they recommend using the Cloning library from Google Code. From personal experience, it deep-copies HashMap
s. It can even clone things that aren't Cloneable
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With