aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections" Watch "Collections" New topic
Author

Collections

Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46
can anyone please explain how equals() work hashCode() method and please give some example so that i can understand
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

http://www.javaranch.com/journal/2002/10/equalhash.html
Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46

i would just want to know about the control flow between equals() and hashCode() method
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

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


OCA7
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 813
    
    1
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

Joined: Dec 08, 2010
Posts: 1512
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 813
    
    1
[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

Joined: Dec 08, 2010
Posts: 1512
    
    5

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.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections