Reputation: 395
I have two sorted sets : sortedSet1(Object ,Object1 , Object2 , Object3 , Object4 )
and sortedSet2(Object ,Object1 , Object2 , Object3 , Object4 )
. The objects have other lists with objects inside.
I want to compare each value from inside out and catch any differences between the 2 sortedSets. I do not want the exact difference. I want to see just if there are any changes. I have tried sortedSet1.equals(sortedSet2)
but it dies not work, if i change a value inside another object inside the sortedSet it does not catch the difference. I have also tried
if (!sortedSet1.containsAll(sortedSet2)) {
// do something
}
with no luck.. I think that the answer exists in streams, but I have very little experience with them..
Thank you very much for your help.
Upvotes: 0
Views: 1062
Reputation: 109547
if i change a value inside another object inside the sortedSet it does not catch the difference
First to find the differences would be something like:
SortedSet<Object> common = new TreeSet<>(sortedSet1);
common.retainAll(sortedSet2);
SortedSet<Object> all = new TreeSet<>(sortedSet1);
all.addAll(sortedSet2);
SortedSet<Object> differences = new TreeSet<>(all);
differences.removeAll(common);
However your mention of changing an object once inside the set, rings an alarm.
One should never change a value so that its used key (hashCode or here Comparable) changes. As then the ordering and structural location is corrupted. For a tree structure parts of the tree become inaccessible as the left subtree is assumed to be less than the node's key, and the right subtree should have nodes greater than the node's key.
Upvotes: 1