Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clone utility for HashMap in java

Tags:

java

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)?

like image 217
Moro Avatar asked Jun 15 '09 21:06

Moro


2 Answers

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.

like image 198
Michael Borgwardt Avatar answered Sep 23 '22 22:09

Michael Borgwardt


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 HashMaps. It can even clone things that aren't Cloneable.

like image 42
Ian Durkan Avatar answered Sep 21 '22 22:09

Ian Durkan