This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes How to check that if same key alreay exists in Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to check that if same key alreay exists in Hashtable" Watch "How to check that if same key alreay exists in Hashtable" New topic
Author

How to check that if same key alreay exists in Hashtable

ram gaurav
Ranch Hand

Joined: Mar 29, 2006
Posts: 208
HI

Like , we make Hashtable t = new Hashtable();

t.put("a","A");
t.put("a","B");

then will it calls the equals methord to check that the key already exists in that or what methord it uses for this.

Thanks
Regards
Gaurav
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You can use containsKey() to check if the key is there beforehand. Or you can look at the return value from put() to see what, if any, value was replaced by your put().

By the way, unless this is legacy code that you cannot change, do not use Hashtable. It is an old class with pointless, slow synchronisation. Use HashMap instead.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
ram gaurav
Ranch Hand

Joined: Mar 29, 2006
Posts: 208
Thanks a lot
 
 
subject: How to check that if same key alreay exists in Hashtable