What role does hashCode play when comparing two objects?

I decided to study some basic Java docs. I started with a class Object

and I am looking at a method equals

. I know what the equals

method is using hashCode

. hashCode

Returns int though , so there is a limited number of unique hash codes that can be generated.

What happens when I try to compare two different objects with the same hashCode

? Is it even possible?

+3


source to share


2 answers


Yes. Two objects can have the same hash code. However, hashcode

it plays no role when comparing two objects. If you want to check if two class objects are equal

, override equals

and determine when two class objects should be considered equal. If you want to compare if one object of a class is smaller / larger than another (usually when sorting a collection), implement Comparable

and override the method compareTo

. (You can also implement Comparator

)

If you want to store an object in, HashSet

or use it as a key in HashMap

, make sure you override the method hashcode

or your objects / keys are likely to be stored in different buckets, resulting in duplicates.



Don't forget to override equals

in the classes you create. If you don't, two object references of your class will be equal if they refer to the same object.

You can read more about equals and hashCode methods in equals and hashCode .

+3


source


The contract for is hashcode()

pretty simple:

  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects should yield the same integer result.

  • It is not required that if two objects are not equal according to the equals method, calling the hashCode method on each of the two objects should produce different integer results.

So a valid hash function for any class of objects could be:

@Override
public int hashcode() {
    return 42;
}

      



The contract that equal objects have the same hash code value is fulfilled.

The problem is that classes that use the aforementioned hashcode to distribute objects in buckets (for example HashSet

) will distribute all objects in the same bucket with severe performance implications. An optimal hash function, while not strictly required, would create different values ​​for unequal objects so that they are distributed in their own buckets.

The contract for Object.equals()

does not require use hashcode()

when comparing. However, if the immutable object has an expensive comparison, it can use the hash code value to determine if the expensive comparison is necessary. The hash code can be cached the first time it is computed. Since the object is immutable, the hashcode cannot change and therefore it can be safely cached in the instance. Therefore, the hashcode can be used as an optimization for comparisons of comparisons: an expensive comparison only needs to be done for instances with the same hashcode.

Simple algorithms for writing adequate hash functions can be found in Effective Java, 3rd Ed. (J. Bloch). A modern IDE can also automatically generate hash functions for you.

0


source







All Articles