• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Overriding Hashcode() and Equals()

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic