How does IntelliJ IDEA generate hash codes?

Here is an example of the 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; } 

Interestingly, there are 31 * and →> 32

+4
source share
2 answers

For →> 32: long and double - 64 bits, since int is 32 bits, so to get the result int, as soon as the change is 32, to save the information.

Multiplication 31 is a typical method. 31 is simple, and repeated multiplication within 2 ^ 32 will go through all the values. So this is great for hashing. (Generally)

+5
source

This follows the principles described in Effective Java (p. 38):

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

+2
source

Source: https://habr.com/ru/post/1381446/


All Articles