Generic HashTable Question

  • Thread starter Thread starter Mythran
  • Start date Start date
M

Mythran

What are HashTable's used for? I mean, what are the advantages over any
other collection type?

Mythran
 
You can access a value by some "name" rather then a numeric index.

For instance, the syntax would be myHashtable["someKey"].

There are times when you need to access elements by the key, for instance
suppose you have a set of employee objects. You might want to add them to a
hashtable if you often access the objects by their social security number.

Now, you could enumerate the elements of a collection to find the employee,
but that would not be efficient. Hashtables are optimized for this purpose
and employ internal data structures that are more complex then normal lists.
 
Mythran said:
What are HashTable's used for? I mean, what are the
advantages over any other collection type?

Lookup of items by key. Access to items over their key is possible in
"constant" time, so there is no need to iterate through the list and check
each item's key until the item with a certain key is found. A hash table is
a large array, if an item is inserted, a hash number of its key is
calculated. This number can be used as index in the large array. When
looking for an item by key, the hash function is evaluated again and the
item in the array is accessed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top