2

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?

aryaxt
  • 76,198
  • 92
  • 293
  • 442

2 Answers2

1

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);
Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
0

You can use serialization/deserialization to do that.

R.Moeller
  • 3,436
  • 1
  • 17
  • 12
  • ...and that requires to implement `Serializable` (and/or `Externalizable`). – Tadas S Nov 19 '13 at 19:15
  • Yes, I don't want to have to do this every time I add a new Model – aryaxt Nov 19 '13 at 20:47
  • 1
    the only other possibility are complete ugly unsafe hacks, which require you to make strong assumptions about the internal memory layout of objects and may break with the next minor hotspot update .. – R.Moeller Nov 19 '13 at 21:10