user590444
user590444

Reputation: 4322

How does IntelliJ IDEA generate hash codes?

Here is example of generated hashCode

 @Override
    public int hashCode() {
        int result;
        long temp;
        temp = x != +0.0d ? Double.doubleToLongBits(x) : 0L;
        result = (int) (temp ^ (temp >>> 32));
        temp = y != +0.0d ? Double.doubleToLongBits(y) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

I wonder what for there 31 * and >>> 32

Upvotes: 4

Views: 1550

Answers (2)

Emmanuel Bourg
Emmanuel Bourg

Reputation: 10998

It follows the guidelines described in the book Effective Java (page 38):

http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109557

For >>> 32: long and double are 64 bits as int is 32 bits, so to get an int result once a shift of 32 must happen to maintain the info.

The multiplication of 31 is a typical technique. 31 is a prime, and repeated multiplication within 2^32 will iterate through all values. So it is great for hashing. (In general)

Upvotes: 5

Related Questions