How do you make a deep copy of an object?

前端 未结 19 1890
执念已碎
执念已碎 2020-11-21 23:09

It\'s a bit difficult to implement a deep object copy function. What steps you take to ensure the original object and the cloned one share no reference?

19条回答
  •  無奈伤痛
    2020-11-21 23:54

    You can do a serialization-based deep clone using org.apache.commons.lang3.SerializationUtils.clone(T) in Apache Commons Lang, but be careful—the performance is abysmal.

    In general, it is best practice to write your own clone methods for each class of an object in the object graph needing cloning.

提交回复
热议问题