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)); }
Fingerprints:
[Bob, Tom, Sam] [Sam, Bob, Tom] true
But 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)); }
he produces:
[Bob, Tom, Sam] [Sam, Bob, Tom] false
My question is one of the clarifications. Can anyone help figure this out? Why consider LinkedHashSet equal, while the same LinkedList will not? I assume the definition of List and Set plays a role, but I'm not sure.
Basically, I say, if you think that Set will be the same, don't you think that List too? And vice versa (in the absence of duplicate elements)?
source share