Collections
  Iterate over a collection
  Use a Hashtable collection
  Choose a collection to use
  Implement a collection
  Clone a collection

Get URL for this page

How Do I....Create and Use a Hashtable?

This example illustrates how to create and use a hash table. A hash table is a collection that consists of key-value combinations, organized into 'buckets' for fast searching. You can search a hash table either by the keys, or by the associated values; however, if fast searching is your objective, searching by key will generally be faster.

 
VB HashTable.aspx

[Run Sample] | [View Source]

As mentioned in the introduction, a hash table is a type of collection. When you populate a hash table, you give it a key to add to the table, and a value that goes with the key. In the following example, employee id numbers are used for the key, and then the employee names, to give you an idea of the kind of combinations you can use. Note that the key does not need to be numeric: any object is acceptable.

Why is a hash table useful? When one of these key-value pairs is added to the hash table, the hash table looks at the key and gives it a hash code, a number that identifies the key (each key you add gets a hash code). These hash codes are then placed in 'buckets' to help organize the entries in the table. This can help later when you try to find something in the table.

Imagine for a second that these buckets aren't used. If you wanted to find something in an array, you would have to go through every element of the array, looking for the element. You have no idea where it is, so there is no fast way to search through the entries. By contrast, a hash table can search for a particular key faster, because once you give it a key it knows which bucket to look in. This means it only has to look through a subset of all the elements.

It is important to remember that this faster searching mechanism is only available when searching by keys. Searching by a value is available, but is potentially slower than key searching. Therefore, where possible you should perform key searches.

To add elements to a hash table, call the Add method, passing in the key-value pairs you want to add.


'  Here we'll create a hashtable of employee numbers and employee names
Dim table As Hashtable = New Hashtable()

'  now, we will add elements to the hashtable, as key-value pairs
table.Add(5123, "Jay")
table.Add(1829, "Tom")
table.Add(2882, "Matt")
VB

Now that the hash table is populated, you can search for elements in it. You are going to search both by key and value. Note that in the actual example, you will accept a value or key to find from the user, so the implementation is slightly different, but the concepts are the same.


'  SEARCHING BY VALUE. Use the method ContainsValue
Dim valueToFind as String = "Jay"

If table.ContainsValue(valueToFind) Then
	Console.WriteLine("Found {0} in the list.", valueToFind )
End If

'  SEARCHING BY KEY. Use the method Contains
Dim keyToFind As Integer = 5123

If table.Contains(keyToFind) Then
	Console.WriteLine("Found {0} in the list.", keyToFind)
End If
VB

Of course, you can do more than simply searching. You can remove entries from the hash table by using the Remove method, which you pass a key identifying the element you want to remove. You can also enumerate over the hash table using the Keys collection or the Values collection inside your instance of the hash table.


'  using the Remove method to delete a particular item...
table.Remove(5123);

Dim o As Object

'  enumerating over the collection with a foreach statement
'  Note the alternate of iterating through by value is demonstrated
'  For Each o In table.Values
For Each o In table.Keys
	Console.WriteLine(o.ToString())
Next
VB

Those are the basics of using a hash table. It is important to know how to iterate through your hash table, once you have it populated with entries. This important topic is covered in the section How Do I...Iterate over a collection? The following code shows you how to use a foreach (For Each in VB) statement to move through our list, and print each of the key-value combinations to the screen. To do this, you also need to reference the Key and Value properties of each element. The following sample demonstrates iterating through the collection.


'  if you are wondering where DictionaryEntry comes from, it is
'  the generic object type of each element in the hashtable
Dim d As DictionaryEntry

For Each d in table
	'  use the Key and Value properties. Note chr(9) is a tab
	Console.WriteLine ("{0}" & chr(9) & "{1}", d.Key, d.Value)
Next d
VB


Copyright 2001-2002 Microsoft Corporation. All rights reserved.