My application stores my web service responses into WeakHashMap. In my application I manipulate the data coming back from the web service in UI, and since the objects are being referenced it also modifies the reference (In my weak hashmap).
Is there a way to store a copy of the objects into my hashmap instead of a reference, without having to implement Clonable on every single Model object in my application?
Kryo allows serialization with minimal effort. It's also should be very efficient as uses direct memory copying with a help of sun.misc.Unsafe. From their quick start:
Kryo kryo = new Kryo();
SomeClass someObject = ...
SomeClass copy1 = kryo.copy(someObject);
SomeClass copy2 = kryo.copyShallow(someObject);
You can use serialization/deserialization to do that.
来源:https://stackoverflow.com/questions/20079923/making-a-copy-of-an-object-dynamically