kostaskougios / cloning

deep clone java objects
Other
591 stars 112 forks source link

Working with cloned java.util.LinkedHashSet #46

Closed antonu17 closed 4 years ago

antonu17 commented 8 years ago

Create new LinkedHashSet and populate it with 2 objects.

Set<Object> set = new LinkedHashSet<>();
set.add(new Object());
set.add(new Object());

Then make deep clone of if

Cloner cloner = new Cloner();
Set<Object> clone = cloner.deepClone(set);

And finnaly pick the first item and try to check if cloned set contains it (or even remove it from there)

Object first = clone.toArray()[0];
clone.contains(first); // returns false
clone.remove(first);  // also returns false and leave set unchanged

PS. It only affects java.util.LinkedHashSet, java.util.HashSet works well, as expected.