# Reverse equals and hashcode contract

Harshana Dias
Ranch Hand
Posts: 348
The contract state below,

If two objects are equal then they should have the same hashcode but the reverse is not true (i.e. If two objects have the same hashcode does not mean that they are equal)

But my question is why the reverse has not given priority.
Same hashcode means same hash bucket. You put object which are equals to the same bucket know?

Anayonkar Shivalkar
Bartender
Posts: 1557
5
Harshana Dias wrote:The contract state below,

If two objects are equal then they should must have the same hashcode but the reverse is not true (i.e. If two objects have the same hashcode does not mean that they are equal)

But my question is why the reverse has not given priority.
Same hashcode means same hash bucket. You put object which are equals to the same bucket know?

As you've stated, same hashCode means same bucket - so, if two objects have same hashCode, this simply means that those objects belong to same bucket. Now, these two objects may be different (i.e. not equal). Thus, if hashCode of two objects are same, doesn't mean that those objects are equal.

I hope this helps.

Phil English
Ranch Hand
Posts: 62
Harshana Dias wrote:The contract state below,

If two objects are equal then they should have the same hashcode but the reverse is not true (i.e. If two objects have the same hashcode does not mean that they are equal)

But my question is why the reverse has not given priority.
Same hashcode means same hash bucket. You put object which are equals to the same bucket know?

Because you cannot guarantee mathematically that two different objects will have different hash codes.

See this.

Also, the HashMap class is capable of coping with hash collisions.

Greenhorn
Posts: 7
The Hashcode groups objects and compares the equals?

Jeff Verdegan
Bartender
Posts: 6109
6
The hashcode is a way to narrow down the search.

Imagine you park your car in a large parking structure with many levels. You make a mental note that you parked on level J. You leave the parking structure for a couple hours, then come back and now it's time to find your car. You don't know exactly where it is, but you remember it's on level J. You go to level J, and start walking down the rows of cars until you find your own. There are many cars on level J, but they're not all equal to yours, so you have to look at them one by one until you find yours. But because you knew it was on level J, you only had to search the cars in level J, instead of looking through the whole parking garage.

Similarly, if you have a million objects in a HashMap, there might be, for example, 10,000 buckets with 100 objects each. The hashCode tells you what bucket to look in, and then you only have to do a linear search of 100 items instead of 1,000,000.

Jeff Verdegan
Bartender
Posts: 6109
6
Adriano Ribeiro wrote:The Hashcode groups objects and compares the equals?

No, the hashCode doesn't "compare the equals." And by itself it doesn't group the objects. What it does do is provide a value that the containing data structure uses to determine which group to put the object in.