File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Hashcode() and Equals() 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 "Overriding Hashcode() and Equals()" Watch "Overriding Hashcode() and Equals()" New topic

Overriding Hashcode() and Equals()

Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105
Hi Ranchers

overridden equals method is called explicitly in the class using
operand1.equals(operand2).My question is when is the overridden Hashcode method gets called .I have seen codes in which just the overridden method
public int hashCode(){return 8;} is there and no calls are made explicitly from the class,even though output depends on the overridden hashCode method(for ex when you add duplicate values in a HashSet).What is the flow for overridden equals and hashcode method in a code.can somebody explain me using an example

Thanks in advance.
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
Hi there,

My first suggestion would be to have look at this topic in K&B book, this is discussed in details overe there.

However, I will try to give you brief idea of hashCode() function.

The rule for hashcode and equal is

"Equal objects must produce the same hash code as long as they are equal, however unequal objects need not produce distinct hash codes."

One of the place I can see this happening is HashMap class in Java API, Lets say you have a Student class and you override equals() method in this method you have logic to determine how two student object from your Student class can be equal. You also override hashCode() and return a constant lets say any number say "8".

Now imagine in you application you have some informtaion per student object and you are storing this information in a hashmap cache where student object is being used as key and some other object being used as value in hashmap.

When you call get() method on hashMap, you need to pass the key in this case it would be object of Student class. On high level Hash Map does following to get the mapping of key on invocation of get().

1. It will call hashCode() method of the Key object to find the hash address of the key in Map.
2. Once the hash adress is located it will check all the key objects stored in that hash bucket using equals() method and the object whose equals() method return true will be return.

Therefore if in your application lets say you are adding Student objects in cache using hashMap at startup and then that map outlived the code which populated that hashmap now to read back those objects from map you need to make sure that if two objects in your Student class are equal they are having same hashcode, otherwise you might not able to get the data from hashMap even if its exists in map.
[ March 10, 2008: Message edited by: yogesh sood ]

If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
subject: Overriding Hashcode() and Equals()
It's not a secret anymore!