In general, donβt worry too much about the hash functions of the standard JDK classes. Even if you can override String (you cannot), in practice, the hash function is almost always "good enough". Perhaps there are a few exceptions - for example, some classes, such as BigInteger and collections, each time calculate their hash code, cyclically moving through each element contained in it, which in some cases is quite false, but how often do you type instances of these classes ?
To develop hash codes for your own classes, the thing you are trying to do extends to the hash codes "randomly" over a range of integers. To do this, you usually need to βmixβ the bit of consecutive fields in your object (you may be interested in an article on my website that clearly illustrates how a String hash code mixes bits ). Multiplying the current hash by an odd number (and usually a prime number), then adding the next element to the hash usually works quite well, like the first attempt. (However, there may be problems with this method when, for example, combined numbers / hash codes tend to have zeros in their low-order bits - usually there is no practical hash function that would absolutely guarantee good work in all cases.)
Then you can check your hash code. Create a series of random objects (or even use some real ones), calculate their hash codes, And from the bottom, say 16 bits of hash codes, and then see how many collisions you get. Make sure that the number of collisions you receive is approximately the number of hash collisions that you expect by chance. . For example, if you And with the lower 16 bits of the hash code (& 0xffff), then after 1000 random objects you expect about 8 collisions. After 2000, you expect about 30 clashes.
As far as performance is concerned, to some extent, I think that getting a hash code that is well distributed will generally be more useful these days than sacrificing hash quality for hash computation speed.
Neil Coffey Mar 26 '09 at 6:26 2009-03-26 06:26
source share