that easiest way of deep cloning (with some performance overhead) is Serialization. It involves serializing the object to bytes and from bytes to object again.
Read full article from How to do deep cloning using in memory serialization in java - How To Do In Java
public SerializableClass deepCopy() throws Exception { //Serialization of object ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(this); //De-serialization of object ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream in = new ObjectInputStream(bis); SerializableClass copied = (SerializableClass) in.readObject(); //Verify that object is not corrupt //validateNameParts(fName); //validateNameParts(lName); return copied; }
No comments:
Post a Comment