-
hashtable
Hello!
I need a answer quick
How can I check if a key is already taken in a hashtable ?
I wanted to loop in all object in the hastable but I can not see how to loop because in hashtable we do not have any index...Any one can help me ?
This is an example of what I want to do:
-----------------------------------------------------
I have added 3 elements in my hastable:
myHashTable.put ("101", Object1);
myHashTable.put ("1241", Object2;
myHashTable.put ("19, Object3;
Now i need a piece of code to loop in the hashtable to be able to check the key of each...how can i do that ???
-
I solved my problem with :
Code:
if(ymhasTable.containsKey ("101")==true)
System.out.println("Key exist");
-
But I still need to find a way to loop in all element of a hashtable so please if you know how to loop through a hashtable please give me some line of code.
-
Hashtable has a keys() method which returns an Enumeration of the keys. You can loop through that and do your comparisions.
:)
-
Thx you I will check this out tommorow, but if you have time I will appreaciate a little example ;)
-
Code:
Hashtable ht = new Hashtable();
//Load Hashtable at some point.
Enumeration e = ht.keys();
while(e.hasMoreElements())
{
System.out.println(e.nextElement().toString());
}
That's how the Enumeration looping will look. Just replace the code inside the while loop with your code.
:)
-
Big thx I have tryed so many thing, but your way is good :) Thx you