wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes is it neccessary to overide the equals and hashcode method of value objects for doing get operation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is it neccessary to overide the equals and hashcode method of value objects for doing get operation?" Watch "is it neccessary to overide the equals and hashcode method of value objects for doing get operation?" New topic
Author

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

utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
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.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29253
    
139

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
Hi Jeanne

Thanks for clarifying that out. I just saw a code snippet on the net which had it like that.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

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!!


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is it neccessary to overide the equals and hashcode method of value objects for doing get operation?
 
Similar Threads
Generix
HasMap Problem using equals ANd hashCode methods
Equals( ) method semantics
why am i getting a null value on fetching from map
default behaviour of hashcode() in object class