8

I have an immutable object, for example a node in the Cartesian space. The class is immutable, so I cache the hashCode for very fast hashing.

private final int hashCode;

private final double x, y, z;

public Node(final double x, final double y, final double z)
{
    this.x = x;
    this.y = y;
    this.z = z;
    this.hashCode = Objects.hashCode(this.x, this.y, this.z);
}

@Override
public boolean equals(final Object obj)
{
    if (this == obj) { return true; }
    if (obj == null) { return false; }
    if (!(obj instanceof Node)) { return false; }
    final Node other = (Node) obj;
    return Objects.equal(this.x, other.x) && Objects.equal(this.y, other.y) && Objects.equal(this.z, other.z);
}

@Override
public int hashCode()
{
    return this.hashCode;
}

Since the hashCode is unique and dependent on all fields of the class AND the class is Immutable, would it be correct to only check Node equality based on the hashCode?

@Override
public boolean equals(final Object obj)
{
    if (this == obj) { return true; }
    if (obj == null) { return false; }
    if (!(obj instanceof Node)) { return false; }
    final Node other = (Node) obj;
    return this.hashCode == other.hashCode;
}

This passes all Unit Tests I have written about the properties of equals() and hashCode() and their interaction, but perhaps there is something I am missing?

Note: Objects.hashCode() and Objects.equal() are Guava classes helpful for the respective methods.

c00kiemon5ter
  • 16,994
  • 7
  • 46
  • 48

2 Answers2

17

Nope; that won't work.

You have 232 possible hashcodes and 2192 possible values.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

No, but..

I guess you could check the hashcode to see whether objects are not equal and gain some performance there:

public boolean equals(final Object obj) {
   if (this == obj) { return true; }
   if (!(obj instanceof Node)) { return false; }
   final Node other = (Node) obj;

   if (this.hashCode != other.hashCode) {
      return false; // If hashcodes differ, we're sure the objects are not equal
   }
   // remainder of the actual equals implementation
}

Of course this will only improve performance in case most of your comparisons yield false. In case of equal objects, this will bring a performance penalty. In your example (comparing just three values), I wouldn't recommend this.

bertvh
  • 821
  • 1
  • 7
  • 17