Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java platform libraries Hashcode

Tags:

java

hashcode

In Effective Java Item 9 (Always override hashCode when you override equals) where it says

Many classes in the Java platform libraries, such as String, Integer, and Date, include in their specifications the exact value returned by their hashCode method as a function of the instance value. This is generally not a good idea, as it severely limits your ability to improve the hash function in future releases.

What does it mean ?

like image 942
peter Avatar asked Oct 06 '22 17:10

peter


1 Answers

It means that you can't rewrite the hash function in later versions of your code to have better hashing properties. For example, the String.hashCode() function is fast...but not very good. But it can't be changed anymore, because the hash code was specified and people have depended on that implementation in their own code.

like image 148
Louis Wasserman Avatar answered Oct 10 '22 04:10

Louis Wasserman