Suppose I need TreeSet
with elements sorted with some domain logic. By this logic it doesn\'t matter order of some elements that doesn\'t equal so compare metho
If you have no specific expected ordering for any two given elements, but still want to consider them un-equal, then you have to return some specified ordering anyway.
As others have posted, hashCode()
isn't a good candidate, because the hashCode()
values of both elements can easily be equal. System.identityHashCode() might be a better choice, but still isn't perfect, as even identityHashCode()
doesn't guarantee unique values either
The Guava arbitrary() Ordering implements a Comparator
using System.identityHashCode()
.