Тёмный

Java Deep Clone an Object 

Mike Møller Nielsen
Подписаться 11 тыс.
Просмотров 4 тыс.
50% 1

Опубликовано:

 

28 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 24   
@devpandya7903
@devpandya7903 11 месяцев назад
Mike really understood the thing. Thank you very much
@MikesTechCorner
@MikesTechCorner 11 месяцев назад
Thank you for watching
@oleksandr3275
@oleksandr3275 Год назад
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. 👍
@MikesTechCorner
@MikesTechCorner Год назад
Glad it was helpful! Thank you for watching and commenting.
@alexanofriev7391
@alexanofriev7391 2 года назад
Thank you for explanation! I am just learning java and your video was the one which finally explained all well and easy to understand!
@MikesTechCorner
@MikesTechCorner 2 года назад
Checkout mapstruct. It will save a ton of time
@PMe-my1td
@PMe-my1td 3 года назад
Thanks...helped me out :-) As a suggestion, maybe spit your code screen so thers slightly less flicking back and forth :-)
@MikesTechCorner
@MikesTechCorner 3 года назад
Noted!
@bobmac3542
@bobmac3542 3 года назад
Thanks, helped me out alot with my uni work :)
@MikesTechCorner
@MikesTechCorner 3 года назад
you are welcome 🙂
@alisbai4376
@alisbai4376 2 года назад
God bless your soul till the end of time. You saved me so much time and effort.
@MikesTechCorner
@MikesTechCorner 2 года назад
Glad it helped! :-)
@damianocaon9293
@damianocaon9293 Год назад
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?
@MikesTechCorner
@MikesTechCorner Год назад
I think I would use the Mapstruct mapping framework. It is great for that purpose.
@SteelPotathor
@SteelPotathor Год назад
Thanks a lot
@MikesTechCorner
@MikesTechCorner Год назад
Thank you for commenting
@chadjensen7248
@chadjensen7248 4 года назад
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 com.fasterxml.jackson.databind.ObjectMapper; .... public static T clone(Object object, Class clazz) { ObjectMapper objectMapper = new ObjectMapper(); T deepCopy = null; try { deepCopy = objectMapper .readValue(objectMapper.writeValueAsString(object), clazz); } catch (JsonProcessingException e) { e.printStackTrace(); } return deepCopy; }
@MikesTechCorner
@MikesTechCorner 4 года назад
Nice!
@MikesTechCorner
@MikesTechCorner 4 года назад
Would you use the deep clone way now instead? Its a bit cheaper as you said. :-)
@chadjensen7248
@chadjensen7248 4 года назад
@@MikesTechCorner I think it kinda depends on the project. If performance is important or if we're paying for cloud resources, then I'd probably pay more attention to saving / squeezing out as much performance as possible. If I was working on a solo project, I'd probably use what's shown in this video. However, I think the utility of the method I posted above is too convenient for me not to use when working in a team. Having to build out a clone for each class type, and then also having to remember to add it to the classes that use that class seems like an easy thing to forget, making the method shown in this video somewhat error prone to engineers who aren't familiar with what the .clone() is doing for that particular object. That said, maybe that's something that just needs to be watched for by more Sr engineers. I think that what's in this video is the most CORRECT way of doing it, but I also think it has issues with failing 'slowly' if devs don't catch improper use of it. Do you think it's always worth doing it the way in your video? Perhaps mine is too hacky , and does even have some issues of its own (e.g - if classes have @JSONIgnore property, they will not be serialized/deserialized properly).
@akshaybabbar3188
@akshaybabbar3188 4 года назад
Nice Video binging on the content.
@MikesTechCorner
@MikesTechCorner 4 года назад
Glad you enjoy it!
@kostasgkoutis8534
@kostasgkoutis8534 2 года назад
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)
@MikesTechCorner
@MikesTechCorner 2 года назад
Yes good points. You can also use mapstruct to copy an object
Далее
ИСТОРИЯ ПРО ШТАНЫ #shorts
00:32
Просмотров 511 тыс.
DEMONS ARE ATTACKING BRAWL STARS!!!
09:08
Просмотров 15 млн
JavaScript Visualized - Execution Contexts
11:41
Просмотров 65 тыс.
The New Option and Result Types of C#
15:05
Просмотров 75 тыс.
All Rust features explained
21:30
Просмотров 317 тыс.
Coding Shorts 110: Garbage Collector and IDisposable
14:25
JAVA DTO Pattern Tutorial | Simplify Your Code
19:12
Просмотров 209 тыс.
Java Equals Verifier Test
21:32
Просмотров 32
ИСТОРИЯ ПРО ШТАНЫ #shorts
00:32
Просмотров 511 тыс.