1

In a TreeMap and a TreeSet, is it okay for compareTo() and equals() be based off of two different fields?

Since both are binary search trees, go left and go right is based on key.Compare(). So theoretically I could do the sorting based on one field (which I will use in compareTo() ) but I could use a separate field for equals() and hashCode().

Is there any harm in doing this? In my understanding, If I use this same object in a HashMap or a HashSet, As long as equals() and hashCode() are based off of the same field, there should not be any issue.

Foo
  • 4,206
  • 10
  • 39
  • 54

0 Answers0