I have a quick question about TreeSet collections and hashCode methods. I have a TreeSet
and I\'m adding objects to it, before I add an object, I check to see i
TreeSet does not use hashCode
at all. It uses either compareTo
or the Comparator you passed to the constructor. This is used by methods like contains to find objects in the set.
So the answer to your question is that your compareTo method or your Comparator are defined so that the two objects in question are considered equal.
From the javadocs:
a TreeSet instance 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 set, equal.