2

In SO I have read several answers related to the implementation of hashcode and the suggestion to use the XOR operator. (E.g. Why are XOR often used in java hashCode() but another bitwise operators are used rarely?).

When I use Eclipse to generate the hashcode function where field is an object and timestamp a long, the output is:

public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result
        + field == null) ? 0 : field.hashCode());
  return result;
}

Is there any reason by not using the XOR operator like below?

  result = prime * result + (int) (timestamp ^ (timestamp >>> 32));
Community
  • 1
  • 1
JohnJohnGa
  • 15,446
  • 19
  • 62
  • 87

1 Answers1

3

Eclipse takes the safe way out. Although the calculation method that uses a prime, a multiplication, and an addition is slower than a single XOR, it gives you an overall better hash code in situations when you have multiple fields.

Consider a simple example - a class with two Strings, a and b. You can use

a.hashCode() ^ b.hashCode()

or

a.hashCode() * 31 + b.hashCode()

Now consider two objects:

a = "ABC"; b = "XYZ"

and

a = "XYZ"; b = "ABC"

The first method will produce identical hash codes for them, because XOR is symmetric; the second method will produce different hash codes, which is good, because the objects are not equal. In general, you want non-equal objects to have different hash codes as often as possible, to improve performance of hash-based containers of these objects. The 31*a+b method achieves this goal better than XOR.

Note that when you are dealing with portions of the same object, as in

timestamp ^ (timestamp >>> 32)

the above argument is much weaker: encountering two timestamps such that the only difference between them is that their upper and lower parts are swapped is harder to imagine than two objects with swapped a and b field values.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523