Java deep copy library

牧云@^-^@ 提交于 2019-11-28 01:13:06

问题


Is there library that can make deep copy?

ex) normal object, array, list, inputstream etc.


回答1:


@Konrad's posting is spot on. The only general way of doing deep copying is to use a Java serialization mechanism.

Obviously, it is expensive.

The other caveat is that some Java objects are impossible to copy by serialization. Examples include

  • Thread and subclasses cannot be serialized because a thread's execution state cannot be serialized.

  • Streams in general cannot be serialized because you cannot get at the state of the stream that has already been written (writers, output streams) or that is yet to be read (readers, input streams). (Indeed, in the reader / input stream case, that state may literally be infinite.)

  • GUI components cannot be serialized because they depend on the (external) graphics environment which can't be serialized.




回答2:


Look for serialization. Java supports it out of the box, but you can also try Hessian, Kryo...

Here's an introduction to Java serialization: http://java.sun.com/developer/technicalArticles/Programming/serialization/

And here's a benchmark done by the Kryo folks: http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking (list of 20 serialization libs)




回答3:


Maybe you can have a look at Dozer.




回答4:


  1. For small objects: copy constructor.
  2. For large objects, whose member references are having tree like structure, go for java serialization.



回答5:


There is a small library that makes it possible to do deep cloning as well as shallow cloning. This answer contains much more details.



来源:https://stackoverflow.com/questions/5001026/java-deep-copy-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!