Wednesday, September 9, 2009

HashCode in Java

HashCode and equals
-------------------
hashcode and equals method is important when we create a Class. When you add a object in Hashtable / HashMap/ Hashset / List this methods will get called and compares whether this object data already exists and equals and it will not override the object.

Override both methods in your class
hashcode returns int
equals returns boolean it will compare the previous objects.

In hashMap if you set the object(put) and if you get the same object from HashMap if you have hashcode and equals you will get the value. Else, you will get null.

Refer below links for more details.
Good example : http://www.javabeat.net/tips/9-hashcode-and-equals-methods.html
// Below script tag for SyntaxHighLighter