• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings and Hashtables

 
Charmaine Gatling
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to compare the keys in a hashtable to a string? I need to find out if any key is found anywhere in a string. I know with a string you can use indexOf() to see if one string is a substring of another but how do you traverse a hashtable in order to do the same thing?
I how this is clear...it's the only way I know how to explain it.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map.keySet() will give you all the keys in the Map. The remaining work is up to you...
[ August 23, 2002: Message edited by: Ilja Preuss ]
 
Dermot Curley
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could do it as follows:
 
Charmaine Gatling
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I'd basically have to store the key into a list (array) in order to compare each one to the string? Then what does the containsValue() method do?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charmaine Gatling:
So, I'd basically have to store the key into a list (array) in order to compare each one to the string? Then what does the containsValue() method do?

Contains value tells you if the object is found in the value part of the hashtables key-value pair. It does not check if the value contains something. It checks to see if the hashtable contains some value.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun recommends using HashMap and Iterator instead of Hashtable and Enumeration, unless you're stuck with a pre-1.2 Java.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic