Search code examples
javacomparable

Is com.google.common.collect.Ordering.arbitrary reliable in a 64-bit address space?


com.google.common.collect.arbitrary() claims to produce a reliable comparison of arbitrary objects via System.identityHashCode(Object). However, since the hashCode is a 32-bit quantity, I don't see how this could work in a 64-bit address space, where there may be more than 2**32 object instances. Is my skepticism justified?


Solution

  • The source code reveals that in case of a collision between two identity hash codes of different objects, the comparator falls back to a map associating each of the objects with a counter that is incremented each time a new object is stored in the map.

    See http://docs.guava-libraries.googlecode.com/git-history/v11.0.1/javadoc/src-html/com/google/common/collect/Ordering.html#line.200