Search code examples
javahashcode

Why java hashcode implementation 31 * x + y is better than x + y?


I'm confused with java interview question about which hashcode implementation is better. We have a class Point {int x, y; }. Why does implementation of hashcode 31 * x + y for this class is better than x + y ? The right answer is "The multiplier creates a dependence of the hashcode value on the order of processing the fields, which ultimately gives rise to a better hash function". But I can't understand why the order of processing is the point here because the entire expression 31 * x + y is calculating when I executes point1.equals(point2); And there is no matter in which order it happens. Am I wrong?


Solution

  • If you use x+y then how to distinguish points (3,4) and (4,3)? Both will have the same hashcode...

    Now while 31 * x + y will not be perfect, in the same case, it will be much much better.

    Note: by definition of hashing there is no perfect hashing. The only thing is to analyse what kind of collisions occurs for a given hash function. In the geometrical case the first one introduces collisions for a very simple and usual symmetry property. Thus in very common cases there could be too much collisions.

    ----EDIT----

    Note that 31x+y ensures that no points in the rectangle (0,0)—(huge value,30) will collide, etc. A point (x,y) will collide only with points (x',y') such that (31x+y=31x'+y') which is a very uncommon geometrical property.