javahashsethashcodehash-code-uniqueness

Uniqueness of hashcode for Java's HashSet<Double> and its subsets


If I have a Java HashSet<Double> with a length ranging from 1 to 10.000, are the hash codes of this set and all of its subsets unique?


Solution

  • No. Object.hashCode() method implementations (e.g. HashSet.hashCode()) are not guaranteed to return a unique value, only a pseudo random value which is good enough for purpose of hash data structures.

    If you nee a unique value for HashSet<Double> based on the set content you should implement it yourself.