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

equals() and hashCode()

K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
I have been using the following code. Is overridding of hashCode() method is just for the sake of definition (if two objcts are equal, then their hashCodes should be equal)even if we don't override hashCode(), the output will be true. It doesn't make any difference. Then why should we override hashCode()? Just for the sake of definition???

Any help on this question would be greatly appreciated. I am breaking my head for the past two days.....

public class equalst
{
int a;
String b;
public equalst(int a,String b)
{
this.a=a;
this.b=b;
}
public static void main(String[] args)
{
equalst t=new equalst(1,"xx");
equalst t1=new equalst(1,"xx");
System.out.println(t.equals(t1)); //true
System.out.println(t.hashCode());
System.out.println(t1.hashCode());

}
public boolean equals(Object obj)
{
if(this == obj)
return true;
if((obj == null) || (obj.getClass() != this.getClass()))
return false;
equalst test = (equalst)obj;
return a == test.a &&
(b == test.b || (b != null && b.equals(test.b)));
}
public int hashCode()
{
return hash*a;
}
}
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi

You have used something as has return hash * a; which is nowhere declared.

Nonetheless hashCode is used to provide a unique code to unique objects. But that's always not possible.

Consider hashCode as a value that should be generally unique for unique objects.

By default an object would inherit the Object's hashCode() method. So it would generate a new hashCode everytime. So if you do not override the default hashCode() implementation and add that Object as a key in HashMap, HashTable etc. you would not be able to retrieve if you loose the current object without going through a lot of hassle(leave it for the time being).

Consider how a HashMap works. A hashmap would have have buckets wherein it would store the key's hashCode associated value in case two or more objects end up having the same hashCode they would lie in the same bucket. Now let say there are 10 keys. 8 have different hashCodes and 2 have the same. When you search for the same hashCode keys the jvm would search the bucket with the same hashCode and see that there are 2 objects. Now the jvm would invoke the equals method to determine which Object is actually required. The Object for which the equals method returns true would be returned.

Hope that was helpful.
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Hi Anupam,

I had constured well from your explanation. If you have any pratical(implementation) example with a sample code, please pass it to me.

Regards,
Kiran.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Kiran

A chapter in "Effective Java" might help understand why we have this contract of equals() and hashCode() methods.

http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf

Thanks
Maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() and hashCode()