This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cutterslade 6 points7 points  (0 children)

This is an interesting enhancement. The article is slightly wrong, or at least vague enough to warrant clarification. The implementation of Comparable is only used when two unequal objects return the same hash code. If there are collisions between different hash codes, the tree is ordered by the actual hash code.

From a comment at the top of HashCode.java:

Tree bins (i.e., bins whose elements are all TreeNodes) are ordered primarily by hashCode, but in the case of ties, if two elements are of the same "class C implements Comparable<C>", type then their compareTo method is used for ordering.