This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hi there I'm having a problem with a hashtable. I use an object called ClientKey as my key value, and this object called Message as my value. So, i create an object called clientkey and put that in the hashtable with a Message object for it's value. Now, if i check to see that the key i just added is still in there i use hashTable's "containsKey()" function and it tells me that the key and it's value are in there. So, here's my problem. I put in a key and a value, but then later on, i create a new key and then try to look it up to see if it's in the hashtable. This should return me true when i call "containsKey()" but it doesn't. If i get an enumeration of all the keys from the hashtable and then compare each to the key that i've created i will get a match, but for some reason "containsKey" returns false. Can anyone give some advice? Thanks Desmond
Perhaps you didn't write an equals(Object) and hashCode() method for your ClientKey class. That would give the behavior I think you described. You are in luck. There is a article on this in this month's JavaRanch newsletter, accessible via www.javaranch.com.
Joined: Oct 13, 2002
Hi John Our ClientKey had a equals method but no hashcode. ONce that was put in things seemed to work. Thanks for the help Desmond
It's important to realize that HashMap and HashSet use the hash method to determine position within the hash object. Failure to override them may mean that your object can't be found in the hash object.