• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals and hash code relation.

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if equals() returns true then hashcodes of both objects should be same.
this is fine.
If hashcodes of two objects are different then equals should return false is it correct??.
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Incorrect.
 
yamini nadella
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How? can some body explain it please. This is what I studied
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
The javadoc for the Object class states that

It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.


Now, why is that?
I am not too sure. My guess has roots in the hashing functions of lore - a hashing function used to provide a way for you to create a small direct-access file out of an enormous but sparse range of indexes. That is - suppose you have an ID number of 10 digits, but you only have about 50 people in your company - it would be a waste of time to have a direct-access file holding place for many more than that.
Therefore you have a hash function that allows two original keys (ID numbers) to have the same target key (a slot in the small file) - when a collision like this occured - the algorithm used to provide for the extra key.
So, when you hash, it is possible for two different objects to have the same hash value (I mean - it is an int value after all, so if you have more than Integer.MAX_VALUE different objects of the same class - you're bound to have different objects return identical hashcodes anyway..
In reality, the hashcode is usually an int representation of the object's address in memory.
Nimo.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi yamini,
I agree with you answer. The equals method on the two objects should return false, If hashcodes of two objects are different.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hashCode should return a "reasonably unique" number in order to provide efficient hashing of the data.
Nowhere does it state that it should be unique with different values for each data content of the hashed object. Furthermore it is left to the programmer/designer to decide on what is "reasonably unique". In some situations that may be the same value for all objects (if there will only ever be a very few of them and calculating a real value would take an inordinate amount of time for example), in others a complex algorithm might be needed.
The rules:
Equals must be symmetrical (iow, a.equals(b) == b.equals(a) for all a and b).
Also, a.hashCode() == b.hashCode() for all a and b for which a.equals(b) == true.
Those are the only rules you are helt to.
Nowhere does it state that a.hashCode() != b.hashCode() for a.equals(b) != true...
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If hashcodes of two objects are different then equals should return false is it correct??.
It is incorrect.
For more explanations i request you to go thru Overriding hashCode() and
equals() in ( K&B) book.
Note: Remember that all that is correct are not legal.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by yamini nadella:
if equals() returns true then hashcodes of both objects should be same.
this is fine.
If hashcodes of two objects are different then equals should return false is it correct??.


Thats right. If two objects have identical hashcodes, they may or may not be equal().
On the other hand, if two objects have different hashcodes, they should not be equal().
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the basic rules for dealing with hash codes.
1. If two objects are equal (by their equals method), they must provide the same hash code.
1a. If two objects return different hash code values, they can not be equal (by their equals method). (This is really the inverse of number 1.)
2. If two objects are not equal, they may or may not have an equal hash code.
In reality, this is a perfectly legal hash code method:

That method will always work for the various rules about hash codes. Unfortunately, this method doesn't lend itself well to hash efficiency. The more "unique" the hash codes are, the more efficient hash tables that hold them will be.
I would suggest taking a look at the API Spec for Object. If you look at the description of the hashcode method, you'll find lots of good information.
Corey
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this &this and if they are of any help.
HTH,
- Manish
reply
    Bookmark Topic Watch Topic
  • New Topic