简体   繁体   中英

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?

No. Object.hashCode() method implementations (eg 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.

The hash code of a set is defined to be the sum of the hash codes of the elements in the set, where the hash code of a null element is defined to be zero .This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two sets s1 and s2 , as required by the general contract of Object.hashCode .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM