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.