• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is it neccessary to overide the equals and hashcode method of value objects for doing get operation?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

On doing a get operation on a hashmap the key objects is taken, its hashcode function is called it's hashcode is compared to the hashcodes of the enetered keys by again calling the hascode function. On a match the key objects equals() function is called and then it is compared to the equals function of the stored key objects on a match again the value object is retrieved. In such a scenario why is it necessary to overide the equals and hashcode methods of the value objects? As the hashcode and equals methods of value object is never called?
please tell me if I am missing something.
Thanks
Utsav.
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not. Where did you read that you have to override equals/hashcode on the value? On the key it is a must - as you noted. But not on the value.

You may need to implement equals/hashcode based on what you later do with the value, but not for the purposes of the map.
 
utsav gupta
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne

Thanks for clarifying that out. I just saw a code snippet on the net which had it like that.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nonetheless over-ridding the equals method is a very normal requirement for most classes even if the class just participates as a 'value' in the hashmap. Of course if you override equals you **must** override hash code!!

 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, the term "value object" should probably be avoided here, as it has two other meanings which have nothing to do with HashMaps. The correct meaning is this one, while the sadly better-known meaning is this one. Note that the second meaning has now been renamed Data Transfer Object, but people still erroneously use the term Value Object here. Anyway, best to avoid the term here as neither meaning seems to be what you mean here.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic