*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Do we need to override the hashCode() method ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Do we need to override the hashCode() method ?" Watch "Do we need to override the hashCode() method ?" New topic
Author

Do we need to override the hashCode() method ?

Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
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

Joined: Aug 30, 2002
Posts: 166
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The answer to your question is that any time you override equals you must override hashCode.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Vivek Nidhi
Ranch Hand

Joined: Aug 10, 2003
Posts: 133
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

Joined: Aug 30, 2002
Posts: 166
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

Joined: Oct 17, 2003
Posts: 78
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.


<b><i>Bengt Hammarlund</i><br />� Sun Certified Java Programmer</b>
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
Thank you very much !!
Jack
Eddie Long
Ranch Hand

Joined: Nov 02, 2003
Posts: 69
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


...and so help me God.
 
Don't get me started about those stupid light bulbs.
 
subject: Do we need to override the hashCode() method ?
 
Similar Threads
Sorted Set Vs Set
Overriding hashCode()
hashcode
Regarding vector
hashCode()