Consider the following SSCCE:
public static void main(String[] args) {
LinkedHashSet<String> set1 = new LinkedHashSet<>();
set1.add("Bob");
set1.add("Tom");
set1.add("Sam");
LinkedHashSet<String> set2 = new LinkedHashSet<>();
set2.add("Sam");
set2.add("Bob");
set2.add("Tom");
System.out.println(set1);
System.out.println(set2);
System.out.println(set1.equals(set2));
}
This prints:
[Bob, Tom, Sam]
[Sam, Bob, Tom]
true
Yet if you change LinkedHashSet
to LinkedList
:
public static void main(String[] args) {
LinkedList<String> set1 = new LinkedList<>();
set1.add("Bob");
set1.add("Tom");
set1.add("Sam");
LinkedList<String> set2 = new LinkedList<>();
set2.add("Sam");
set2.add("Bob");
set2.add("Tom");
System.out.println(set1);
System.out.println(set2);
System.out.println(set1.equals(set2));
}
it produces:
[Bob, Tom, Sam]
[Sam, Bob, Tom]
false
My question is one of clarification. Can someone help make sense of this? Why would a LinkedHashSet
be considered equals whereas the same LinkedList
would not? I'm assuming the definition of List
and Set
plays a role, but I'm not sure.
Basically, I'm saying if you consider the Set
s to be the same, wouldn't you consider the List
s to be the same too? And vice-versa (assuming no duplicate elements)?
The guarantee that
LinkedHashSet
makes is about iteration order. However, it's still aSet
and a set doesn't care about order in itself. AList
on the other hand, does. AList
with an element in 3rd position is not the same as anotherList
with the same element in the 1st position.Set
javadoc for theequals(Object)
methodThe
LinkedHashSet
javadoc statesA
LinkedHashSet
is aSet
. It has the same rules, ie. those that apply to the set ADT.As mentioned above: LinkedHashSet extends HashSet which extends AbstractSet which implements equals method: https://docs.oracle.com/javase/8/docs/api/java/util/AbstractSet.html#equals-java.lang.Object-
The easiest way to compare LinkedHashSet if order if important to you is to serialize it and compare them then:
Result: