-
HashTable question
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,
-
Re: HashTable question
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...
Code:
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"]);
-
Re: HashTable question
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.
-
Re: HashTable question
...and the same goes for ArrayList. Java uses list.get(index) whereas C# uses the array syntax, list[index]