I am just trying to find out the best solution how to make a deep copy of HashMap
. There are no objects in this map which implement Cloneable
. I would like to find better solution than serialization and deserialization.

- 10,858
- 13
- 45
- 84

- 882
- 5
- 13
- 34
-
5So this is more of a question of how to clone un-cloneable objects? The HashMap seems like a minor detail that could be addressed by simple recursion – Matt Whipple Oct 24 '12 at 12:32
-
Whose object classes are they? If your own, just add Cloneable. – Hot Licks Oct 24 '12 at 12:35
-
2Trying to clone objects which are not Cloneable is a definite code smell. This will be a good time to consider refactoring. – amit Oct 24 '12 at 12:37
-
If the objects are immutable, you very unlikely need to/should clone them. Are they? – Bruno Grieder Oct 24 '12 at 12:41
-
The Object which is needs to be copied is autogenerated. I cant modified this Object. I have Map
which i dont have any idea what objects it will holds. – Smolda Oct 24 '12 at 12:46 -
2@Smolda - if you have no idea what the objects are, then this smells: you cannot guarantee that you will be able to clone: what it the object holds a `transient` property, or a `stream`, etc... – Bruno Grieder Oct 24 '12 at 12:48
-
I agree with you but there is no way out at this moment.. since Its not my ever.. :( – Smolda Oct 24 '12 at 14:46
-
1There are objects which are specifically designed so you can't get more than one of them. You _cannot_ make copies of arbitrary objects. Find another way. – Louis Wasserman Oct 24 '12 at 16:53
-
Possible duplicate of [Assigning Hashmap to Hashmap](http://stackoverflow.com/questions/11296490/assigning-hashmap-to-hashmap) – naXa stands with Ukraine Oct 21 '15 at 13:19
3 Answers
Take a look at Deep Cloning, on Google Code you can find a library. You can read it on https://github.com/kostaskougios/cloning.
How it works is easy. This can clone any object, and the object doesnt have to implement any interfaces, like serializable.
Cloner cloner = new Cloner();
MyClass clone = cloner.deepClone(o);
// clone is a deep-clone of o
Be aware though: this may clone thousands of objects (if the cloned object has that many references). Also, copying Files or Streams may crash the JVM.
You can, however, ignore certain instances of classes, like streams et cetera. It's worth checking this library and its source out.

- 7,238
- 9
- 41
- 64

- 10,858
- 13
- 45
- 84
-
Would it deep copy a object and all its nested members? Say Object1 has Object2, Object2 has a Map
. When I do a `cloner.deepClone(Object1);`, will it deep copy all? – zengr Sep 22 '15 at 22:39 -
I don't think it can be implemented in a generic way.
- If you have the chance to simply implement clone, I'd go that way.
- A bit more complex is creating a type map, where you look up some kind of clone implmentation class based on the class of each object
- When the objects might form a Directed Acyclic Graph, I'd in general keep a Map from the original to the clone of every object I've ever seen, and check if I've already made it
- When you have a general graph, the problem gets really nasty. You might have strange constraints of the object creation order, it might even be impossible when you have final fields.
For now, I'd propose to re-write your question in a less general way

- 1,324
- 1
- 11
- 21
This is not easy, we are using some sort of workaround:
1) convert the map to json string. (for example, using Google Gson)
2) convert the json string back to map.
Do note that there is performance issue, but this is kind of easiest way.

- 10,725
- 19
- 102
- 158