aspose file tools*
The moose likes Beginning Java and the fly likes Reverse equals and hashcode contract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reverse equals and hashcode contract " Watch "Reverse equals and hashcode contract " New topic
Author

Reverse equals and hashcode contract

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
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

Joined: Dec 08, 2010
Posts: 1512
    
    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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Phil English
Ranch Hand

Joined: Jun 18, 2012
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.
Adriano Ribeiro
Greenhorn

Joined: Nov 16, 2012
Posts: 7
The Hashcode groups objects and compares the equals?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
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

Joined: Jan 03, 2004
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.
Adriano Ribeiro
Greenhorn

Joined: Nov 16, 2012
Posts: 7
Got it. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reverse equals and hashcode contract