Consider the below code which creates a SortedSet
using a comparator based on string length
public class SortedSetTest {
public static void main(String[] args) {
SortedSet<String> sortedSet = new TreeSet<>(Comparator.comparing(String::length));
sortedSet.addAll(Set.of("aa", "bb"));
System.out.println(sortedSet);
}
}
The output of the above is
The bb
element disappears, breaking the Set
contract. The comparator is supposed to only sort the elements and not distinguish them from one another, which is what equals does in all the collections.
On the other hand, if I enhance the comparator to always return non-zero for unequal items like below, only then do I get the correct results.
public class SortedSetTest {
public static void main(String[] args) {
SortedSet<String> sortedSet = new TreeSet<>(Comparator.comparing(String::length)
.thenComparing(String::toString));
sortedSet.addAll(Set.of("aa", "bb"));
System.out.println(sortedSet);
}
}
The output now is [aa, bb]
as I would expect.
The question is, why does SortedSet
ignore the equals
method in first place and removes an unequal object from the set?
The comparator
method inside the SortedSet
interface is documented as follows:
Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
The above indicates that the comparator is only used to order the elements in the set and not to distinguish them from one another, which is what equals
is for in all the collections.
Digging into the javadoc further, it turns out that the above javadoc comment is incorrect, because there is another side note that contradicts it:
Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided) must be consistent with equals if the sorted set is to correctly implement the Set interface. (See the Comparable interface or Comparator interface for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a sorted set performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the sorted set, equal. The behavior of a sorted set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S.