I've decided to study some primary documentation in Java. I've started with Object
class and I am looking at the equals
method. I know that equals
is using hashCode
method. Although, hashCode
returns int so there are a limited number of unique hashcodes that can be generated.
What will happen when I try to compare two diffrent objects that have the same hashCode
? Is this even posible?
Yes. Two objects can have the same hashcode. However, hashcode
plays no role when comparing two objects. If you want to check if two objects of a class are equal
, override equals
and define when two objects of the class should be considered equal. If you want to compare if one object of a class is less than/greater than the other (usually while sorting collection), implement Comparable
and override the compareTo
method. (You can also implement Comparator
)
If you ever want to store an object in a HashSet
or use it as a key in a HashMap
, make sure that you override the hashCode
method or your objects/keys will most likely be stored in different buckets resulting in duplicates.
Don't forget to override equals
in classes that you create. If you don't do this, two references to an object of your class will only be equal if they refer to the same object.
You can read more about the equals and hashCode methods in the equals and hashCode documentation.
The contract for hashcode()
is quite simple:
If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results.
A valid hash function for any class of objects could therefore be:
@Override
public int hashcode() {
return 42;
}
The contract that equal objects have the same hashcode value is satisfied.
The problem is that classes that use the above hashcode to distribute objects into buckets (eg. HashSet
) would distribute all objects into the same bucket with severe performance consequences. An optimal hash function, though not strictly required, would produce distinct values for unequal objects so that they would be distributed to their own buckets.
The contract for Object.equals()
does not require the use of hashcode()
when making comparisons. However, if an immutable object has an expensive equals comparison, it may use the hashcode value to determine whether an expensive comparison is necessary. The hashcode can be cached the first time it is computed. Because the object is immutable, the hashcode can not change and so it may be safely cached within the instance. The hashcode can therefore be used as an optimization for equals comparisons: the expensive comparison only need be done on instances with the same hashcode.
Simple algorithms for writing adequate hash functions can be found in Effective Java, 3rd Ed. (J. Bloch). Modern IDE's can also automatically generate hash functions for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With