Click to See Complete Forum and Search --> : HashTable question


doglin
October 16th, 2005, 10:06 PM
Hi :
Java's implementation of Hashtable provides the users with the public Object get(Object key)So if the user wants to retrieve the object value in the existing HashTable, they just need to provide the key, and the operation time is O(1).

However, I don't see the similar method in C#, it only has getEnumerator which will go through the entire table. and the timing is O(N)

I would like to know if there is something similar to the get(Object key) method I can use and has a time of O(1).


Many thanks,

Jason Isom
October 16th, 2005, 10:43 PM
The following code will print 17 in constant amount of time...I think. I'm pretty sure it's a requirement that retrievals from a Hashtable take O(1) time, else I'm not really sure what distinguishes a HashTable...


Hashtable ht = new Hashtable();
ht.Add("Jason", 23);
ht.Add("Josh", 23);
ht.Add("Jacob", 7);
ht.Add("Jeremy", 17);

System.Console.WriteLine(ht["Jeremy"]);

MadHatter
October 16th, 2005, 11:24 PM
hashtable has a special enumerator IDictionaryEnumerator for obtaining / iterating through a list of keys or dict entries, and the actual get(key) function uses normal hashing to obtain the value.

Norfy
October 17th, 2005, 08:26 AM
...and the same goes for ArrayList. Java uses list.get(index) whereas C# uses the array syntax, list[index]