tag:blogger.com,1999:blog-8712770457197348465.post6825435676077841416..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: How to Clone a Collection in Java? Deep copy of ArrayList and HashSet Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-8712770457197348465.post-64187507376759754872018-09-23T20:21:15.911-07:002018-09-23T20:21:15.911-07:00@Rahul - you have created the copy of employee bef...@Rahul - you have created the copy of employee before adding elements in original employee object.Anonymoushttps://www.blogger.com/profile/06630390464370483779noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-90279706890055933932018-01-17T07:13:21.828-08:002018-01-17T07:13:21.828-08:00anybody knows why my shallow copy is not working ?...anybody knows why my shallow copy is not working ?<br />List original = new ArrayList<>();<br /><br /> Employee employee1 = new Employee("Rahul", "Engineer");<br /> Employee employee2 = new Employee("Anil", "Engineer");<br /> Employee employee3 = new Employee("Raju", "Engineer");<br /><br /> List copy = new ArrayList<>(Rahul ghttps://www.blogger.com/profile/03661737555480556775noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-71044351423060389172017-01-09T12:26:57.742-08:002017-01-09T12:26:57.742-08:00Great Post. can you Please tell me
1.if Employee ...Great Post. can you Please tell me <br />1.if Employee Class has Address Class object inside it. then what should we do to deep clone?<br />2.we always need Address class clone able? , What if it is not?Anonymoushttps://www.blogger.com/profile/00518508696256983538noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-53717788567259412472016-12-08T17:20:49.445-08:002016-12-08T17:20:49.445-08:00@Anonymous, both lists are actually pointing to sa...@Anonymous, both lists are actually pointing to same String objects e.g. "Joe", "Tim" and "Frank", it means they are not exactly deep cloning, but given they are Immutable and cannot be changed, one list will not be affected by other when someone modify their elements. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21715151072059827582016-12-06T23:14:07.623-08:002016-12-06T23:14:07.623-08:00Copy Constructor of List/Set provides a different ...Copy Constructor of List/Set provides a different List/Set. And hence the deep cloning can be achieved using the copy constructor. [Provided => The List/Set onto which the cloning is performed does not contain any mutable element into them] e.g.<br /><br />Collection org = new HashSet<>(); <br /> org.add("Joe"); <br /> org.add("Tim"); <br /> org.add("Frank&Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54703683800070560732014-03-15T02:45:13.435-07:002014-03-15T02:45:13.435-07:00Shallow cloning copies the top level of a tree, bu...Shallow cloning copies the top level of a tree, but anything pointed to from that top level (e.g., object properties) remains shared by both copies. Deep cloning copies all levels of the tree, leaving no links between the source and the copy.<br /><br />For instance, say you have a Person object (a) with a spouse property, which is also a Person object:<br /><br />+-------------+<br />| Person: aAnonymoushttps://www.blogger.com/profile/03451697247168561575noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-19576211163810895542014-03-13T21:47:30.069-07:002014-03-13T21:47:30.069-07:00Is deep copy more fast than create new object and ...Is deep copy more fast than create new object and set value?Anonymoushttps://www.blogger.com/profile/16810901175589106515noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-69395834740309619782014-03-12T08:03:10.770-07:002014-03-12T08:03:10.770-07:00You are absolutely correct Santosh. Thanks for poi...You are absolutely correct Santosh. Thanks for pointing this out.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-17476341672002653782014-03-11T20:38:49.692-07:002014-03-11T20:38:49.692-07:00That's all on How to clone Collection in Java....That's all on How to clone Collection in Java. Now we know that copy constructor or various collection classes e.g. addAll() method of List or Set, only creates shallow copy of Collection. I think last statement should be like that.<br /><br />Santihttps://www.blogger.com/profile/00727974038735630972noreply@blogger.com