Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are two Java objects with same hashcodes not necessarily equal?

Tags:

I understand why providing same hashcode for two equal (through equals) objects is important. But is the vice versa true as well, if two objects have same hashcode do they have to be equal? Does the contract still hold? I cannot find an example where this could happen, because if all those attributes that are taking part in equals method are being used to override hashcode method as well then we will always same hashcode of objects that are equal. Please comment.

like image 652
Abidi Avatar asked Mar 26 '11 15:03

Abidi


People also ask

Can two unequal objects have same hashcode in Java?

Unequal objects must have different hash codes – WRONG! Objects with the same hash code must be equal – WRONG!

What happens when two objects have same hashcode?

1) If two objects are equal (i.e. the equals() method returns true), they must have the same hashcode. 2) If the hashCode() method is called multiple times on the same object, it must return the same result every time. 3) Two different objects can have the same hash code.

Can 2 unequal objects have the same hashcode if its function is implemented correctly?

It is perfectly legal for two objects to have the same hashcode. If two objects are equal (using the equals() method) then they have the same hashcode. If two objects are not equal then they cannot have the same hashcode.

Can two different strings have same hashcode?

If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code.

Do all objects in Java have an equals method?

Every Object in Java includes an equals() and a hashcode() method, but they must be overridden to work properly. To understand how overriding works with equals() and hashcode() , we can study their implementation in the core Java classes. Below is the equals() method in the Object class.

Can HashMap have same hashcode?

Since, hashmap searches key/value pair based on hashcode of key. Hashmap will find 209 hashcode for key "b". Since, same hashcode is found for key "a", Hashmap may return value "aValue" instead of expected value "bValue".


2 Answers

If two objects have the same hashcode then they are NOT necessarily equal. Otherwise you will have discovered the perfect hash function.

But the opposite is true: if the objects are equal, then they must have the same hashcode.

like image 93
Petar Minchev Avatar answered Nov 10 '22 18:11

Petar Minchev


The purpose of the hashCode function is allow objects to be quickly partitioned into sets of things that are known to unequal to all items outside their own set. Suppose one has 1,000 items and one divides them into ten roughly-equal-sized sets. One call to hashCode could quickly identify the item as being not equal to 900 of the items, without having to use equals on any of those items. Even if one had to use equals to compare the item to 100 other items, that would still be only 1/10 the cost of comparing it to all 1000 items. In practice, even in a large collection, hashCode will often eliminate 99.9% or more of the unequal items, leaving at most a handful to be examined.

like image 20
supercat Avatar answered Nov 10 '22 18:11

supercat