Paul Clapham wrote:And you can see it returning zero?
Assuming your answer is yes, I'm going to suggest that you should override the equals() method of TestClass to return true if the two TestClass objects are "equal", following the same rules you put into your comparator.
Doug Morand wrote:Seems that the TreeMap is a bit tricky to implement.
Jeff Verdegan wrote:
Doug Morand wrote:Seems that the TreeMap is a bit tricky to implement.
I doubt you're implementing TreeMap. More likely you're using it. And it's not hard to use at all. But you have to use it consistently. For instance, checking for something being contained should be consistent with it being equal to something else in your tree as per the comparator.
Doug Morand wrote:I was under the impression that I would use the Comparator to sort the list properly, but now I'm seeing that it's being used to determine if the key is contained within the Set
Doug Morand wrote:Does someone have an example of how to use a TreeMap to not insert duplicates, but sort on a different value from the key?
...a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal.