• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections

 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone please explain how equals() work hashCode() method and please give some example so that i can understand
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i would just want to know about the control flow between equals() and hashCode() method
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See K&B page 544 and 549.If you want to learn any concept then you have to read yourself.Here you can post your doubts.If still you want whole explanation about that, then wait, it may be anyone has free time to explain whole story that is already written in book.

-hth
 
Himai Minh
Ranch Hand
Posts: 1294
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh KumarPandey wrote:can anyone please explain how equals() work hashCode() method and please give some example so that i can understand


equals() vs hashCode():

equals() defines how to compare two objects to see if they are equals. Example: If two students have the same ID attribute, then they are equal.
hashCode() defines where to put an object in a hash set. Hash set works like an array. If one student has hashCode = 1, this student object is put in array[ 1]. If another one has hashCode = 2, it is put in array[2]....
You can define how to compute hashcode in hashCode() method.

Given a student object. To hash it in hash set, first compute this student's hashcode. If it is 1, check array[1]. If array[1] student has the same ID as this student object, then don't put it in hashset because hash set won't allow duplicated object.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:
Suresh KumarPandey wrote:can anyone please explain how equals() work hashCode() method and please give some example so that i can understand


equals() vs hashCode():

equals() defines how to compare two objects to see if they are equals. Example: If two students have the same ID attribute, then they are equal.
hashCode() defines where to put an object in a hash set. Hash set works like an array. If one student has hashCode = 1, this student object is put in array[ 1]. If another one has hashCode = 2, it is put in array[2]....
You can define how to compute hashcode in hashCode() method.

Given a student object. To hash it in hash set, first compute this student's hashcode. If it is 1, check array[1]. If array[1] student has the same ID as this student object, then don't put it in hashset because hash set won't allow duplicated object.

Well, my understanding differs from above (or I didn't understand what Himai is trying to say).

From Java point of view, a collection contains lot of objects, and those objects are divided in various buckets. Each bucket may contain several objects. Only rule is - all the object belonging to same bucket must have same hashcode.

Thus, hashCode method is basically used to identify which bucket the collection should search.

Now, as mentioned, that bucket may contain various objects, so, how to find the exact object which we are looking for? This is when equals method comes into play.

The hashCode and equals contract is as follows:
1) If A.equals(B) returns true, then hashCode of A and B must be same.
2) If hashCode of A and B is different, then A.equals(B) must return false.

For more information on hashCode and equals, I would suggest to SearchFirst. Also, please refer an excellent book - Effective Java by Joshua Bloch.

I hope this helps.
 
Himai Minh
Ranch Hand
Posts: 1294
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Anayonkar Shivalkar
The hashCode and equals contract is as follows:
1) If A.equals(B) returns true, then hashCode of A and B must be same.
2) If hashCode of A and B is different, then A.equals(B) must return false.



This hashCode and equals contract is a requirement for hash set /map.
In general, can we define all objects with the same hashcode? Yes.
All objects can have hash code =1 for example.
In general, we can also define unique hash codes for objects.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote: In general, can we define all objects with the same hashcode? Yes.

Please note that this is not a good idea to do this, but you are still following the contract .

The contract says - if A.equals(B) returns true, then hashCode of A and B must be same. However, the corollary is not true (i.e. just because hashCode of two objects are same, doesn't necessarily mean that equals method should return true).

By having same hashCode for all objects, what you are doing is - you are simply having a single bucket, and all objects are being kept in that. So, there will be more work to do by equals method.

Let me reiterate - this is inefficient, yet legal way.

The actual problem starts when you break the contract (e.g. equals method returns true, but hashcodes are different etc.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic