• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do we need to override the hashCode() method ?

 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
Other than Collections would use object's hashCode() method, do we need to override the hashCode() method ? If so, when to use the method ?
Thanks in advance!
Jack
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I create 2 String objects, s1 and s2, both store same value "abc", I put both of them into Hashtable, ht.put(new Integer(1), s1), ht.put(new Integer(2), s2) respectively. Are both of the String objects put into the same location inside Hashtable?
Thanks in advance!
Jack
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, because the keys objects on which hashcode is invoked are the Integers.
Notice that a program is the best way to solve many questions. Or just reading the API.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to your question is that any time you override equals you must override hashCode.
 
Vivek Nidhi
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there
Study Material
Check this link u can have a nice understanding of these stuff
Regs
Vivek Nidhi
[ November 01, 2003: Message edited by: Vivek Nidhi ]
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that means, if I just want to compare whether two objects are equal, it is enough to override the equals() method only. If I put objects into a Collections object, I need to override equals() and hashCode() method ?
Thanks,
Jack
 
bengt hammarlund
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack:
It's in the contract of Object class API that if two objects are considered equal by the equals() method then their hashCode() method HAS TO return the same integer. So, if you want to have your code to be considered any decent by anyone else, you should stick to this contract. That means that everytime you override equals() you HAVE TO override hashCode() in order to stick to the contract. Otherwise, clients of your class (other people that will use your class, or even YOU) will expect some behavior from your class when they put it in a HashMap collection for example - and your code will NOT behave like it should.
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much !!
Jack
 
Eddie Long
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jack Lau:
If I create 2 String objects, s1 and s2, both store same value "abc", I put both of them into Hashtable, ht.put(new Integer(1), s1), ht.put(new Integer(2), s2) respectively. Are both of the String objects put into the same location inside Hashtable?
Thanks in advance!
Jack

---------------------------------
It probably would put both strings in the same location in the hashtable because the hashcode MAY evaluate to the same value.
By <u>location</u> i mean the pool of values in the hashtable that a particular key refers to and i use MAY because the algorithm used to calculate the hashcode may be dynamic e.g. It use a value that is generated at run time to work out the hashcode.
Hope this helps
Goodluck
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic