Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkedHashSet .equals() vs LinkedList .equals() with same elements but 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));
}

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 Sets to be the same, wouldn't you consider the Lists to be the same too? And vice-versa (assuming no duplicate elements)?

like image 977
ryvantage Avatar asked Apr 25 '14 20:04

ryvantage


People also ask

What is the difference between LinkedHashSet and LinkedList?

Java LinkedList class uses doubly linked list to store the elements while LinkedHashSet uses LinkedHashMap internally to store it's elements. uniqueness: LinkedList class can contain duplicate elements while LinkedHashSet contains unique elements only like HashSet.

Is LinkedHashSet an ordered collection?

The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted.

Does LinkedHashSet maintain insertion order?

LinkedHashSet maintains insertion order, just like an ArrayList .

What is a LinkedHashSet How is different from a HashSet?

HashSet is an unordered & unsorted collection of the data set, whereas the LinkedHashSet is an ordered and sorted collection of HashSet. HashSet does not provide any method to maintain the insertion order. Comparatively, LinkedHashSet maintains the insertion order of the elements.


1 Answers

The guarantee that LinkedHashSet makes is about iteration order. However, it's still a Set and a set doesn't care about order in itself. A List on the other hand, does. A List with an element in 3rd position is not the same as another List with the same element in the 1st position.

Set javadoc for the equals(Object) method

Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set). This definition ensures that the equals method works properly across different implementations of the set interface.

The LinkedHashSet javadoc states

Hash table and linked list implementation of the Set interface, with predictable iteration order.

A LinkedHashSet is a Set. It has the same rules, ie. those that apply to the set ADT.

like image 131
Sotirios Delimanolis Avatar answered Oct 27 '22 07:10

Sotirios Delimanolis