Java Deep Clone an Object

preview_player
Показать описание
Enjoy! :-)
Thank you for commenting and asking questions.

Library sign up referral link:

Checkout or buy the Introduction to Algorithms book at Amazon:

The code is located here:
Follow me on twitter:
Chat on Discord:
Support me on Patreon:

As an Amazon Associate I earn from qualifying purchases.
Check out the stuff that I like (always updated):
Рекомендации по теме
Комментарии
Автор

Dear Mike !
Thank you for excelent explanation of deep cloning, i liked how you explained with "mistakes" of not implementing Cloneable, so that other users can see which mistakes can appear on the way of deep cloning.
👍

oleksandr
Автор

Mike really understood the thing. Thank you very much

devpandya
Автор

Thank you for explanation! I am just learning java and your video was the one which finally explained all well and easy to understand!

alexanofriev
Автор

God bless your soul till the end of time. You saved me so much time and effort.

alisbai
Автор

Thanks, helped me out alot with my uni work :)

bobmac
Автор

Thanks...helped me out :-)
As a suggestion, maybe spit your code screen so thers slightly less flicking back and forth :-)

PMe-mytd
Автор

Hi, thank you for the excellent video! There's only one thing I can't understand. If I have to clone a class that extends a library class (for example I have piece, created by me, that extends Rectangle by JavaFX), how can I clone that class?

damianocaon
Автор

A way I've done this is to serialize the entire object to a JSON string using Jackson objectMapper. I then deserialize it into into a new object using that JSON string. It's maybe a little expensive, but it is actually pretty universal code that doesn't require me to pollute my model classes.
This code:
import
....
public static <T> T clone(Object object, Class<T> clazz) {

ObjectMapper objectMapper = new ObjectMapper();
T deepCopy = null;
try {
deepCopy = objectMapper
.readValue(objectMapper.writeValueAsString(object), clazz);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return deepCopy;
}

chadjensen
Автор

I was expecting the copy constructor pattern to show up. Also you forgot to mention the issue that arises when there is a cyclical reference between the original class and its captured reference (Spaceship refers to Captain, Captain refers to Spaceship)

kostasgkoutis