Deep clone实用程序推荐 [英] Deep clone utility recommendation

查看:490
本文介绍了Deep clone实用程序推荐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何用于深度克隆java集合的实用程序:

Is there any utility for deep cloning for java collections:


  • 数组

  • 列表

  • 地图

注意:不使用序列化,而是使用Object,更喜欢某种解决方案。 clone()方法。我可以确定我的自定义对象将实现clone()方法,并且只使用可克隆的java标准类...

NOTE: prefer some solution without usage of serialization, but with use of Object.clone() method. I can be sure that my custom object will implement clone() method and will use only java-standard classes that are cloneable...

推荐答案

我认为以前的绿色答案很糟糕,为什么你会问?


  • 它增加了很多代码

  • 它要求您列出要复制的所有字段并执行此操作

  • 当使用clone()$ b时,这不适用于列表$ b(这就是HashMap的clone()所说的:返回这个HashMap实例的浅表副本:键和值本身没有克隆。)所以你最终手动完成它(这让我哭了)

哦顺便说一下序列化也很糟糕,你可能不得不在整个地方添加Serializable(这也让我哭了)。

Oh and by the way serialization is also bad, you might have to add Serializable all over the place (this also makes me cry).

那么解决方案是什么:

Java深度克隆库
克隆库是一个小型的开源(apache许可证)java库,深层次一些对象。对象不必实现Cloneable接口。实际上,这个库可以克隆任何java对象。如果您不希望修改缓存对象或者只是想要创建对象的深层副本,则可以在缓存实现中使用它。

Java Deep-Cloning library The cloning library is a small, open source (apache licence) java library which deep-clones objects. The objects don't have to implement the Cloneable interface. Effectivelly, this library can clone ANY java objects. It can be used i.e. in cache implementations if you don't want the cached object to be modified or whenever you want to create a deep copy of objects.

Cloner cloner=new Cloner();
XX clone = cloner.deepClone(someObjectOfTypeXX);

https://github.com/kostaskougios/cloning

这篇关于Deep clone实用程序推荐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆