LinkedHashSet.equals () vs LinkedList.equals () with the same elements but with a different order

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)?

+6
source share
2 answers

The guarantee that LinkedHashSet does is the iteration order. However, this is still Set , and the set does not care about the order in itself. A List , on the other hand, does. A List with an element in the 3rd position does not coincide with another List with the same element in the 1st position.

Set javadoc for equals(Object) method

Returns true if the specified object is also a set, two sets are of the same size, and each member of the specified set is contained in this set (or, which is the same, each member of this set is contained in the specified set). This definition ensures that the equals method works correctly in different implementations of the installed interface.

LinkedHashSet javadoc state

A hash table table and an implementation of a linked list of the Set interface, with a predictable iteration order.

A LinkedHashSet is a Set . It has the same rules, i.e. those that apply to the installed ADT.

+10
source

As mentioned above: LinkedHashSet extends HashSet, which extends AbstractSet, which implements the equals method: https://docs.oracle.com/javase/8/docs/api/java/util/AbstractSet.html#equals-java.lang.Object -

Compares the specified object with this set for equality. Returns true if the given object is also a set, two sets are the same size, and each member of this set is contained in this set. This ensures that the equals method works correctly in different implementations of the Set interface.

The easiest way to compare a LinkedHashSet if it’s more important for you to serialize it and compare them:

  LinkedHashSet<Integer> reverseOrder = new LinkedHashSet<>(); reverseOrder.add(2); reverseOrder.add(1); LinkedHashSet<Integer> ordered = new LinkedHashSet<>(); ordered.add(1); ordered.add(2); System.out.println("Equals via set: " + ordered.equals(reverseOrder)); System.out.println("Equals With Arrays: " + ordered.ordered.toString().equals(reverseOrder.ordered.toString())); 

Result:

 Equals via Set: true Equals With Arrays: false 
0
source

All Articles