本文介绍了动态地创建对象的副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序将我的Web服务响应存储到WeakHashMap中。
在我的应用程序中,我操作从UI中的Web服务返回的数据,并且由于对象被引用,它还修改引用(在我的弱hashmap)。

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

有没有办法将对象的副本存储到我的hashmap而不是引用,而不必在我的应用程序中的每个单独的Model对象上实现Clonable?

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?

推荐答案

允许以最小的努力进行序列化。它也应该非常有效,因为使用直接内存复制帮助 sun.misc.Unsafe 。从他们的快速开始:

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

这篇关于动态地创建对象的副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:47