As you saw, the ArrayList addresses most of the problems of the Array class, while it supports all the convenient array features. Yet, the ArrayList, like the Array, has a major drawback: You must access its items by an index value. Another collection, the HashTable collection, is similar to the ArrayList, but it allows you to access the items by a key.
Each item in a HashTable has a value and a key. The value is the same value you’d store in an array, but the key is a meaningful entity for accessing the items in the collection, and each element’s key must be unique. Both the values stored in a HashTable and their keys can be objects. Typically, the keys are short strings or integers.
The HashTable collection exposes most of the properties and methods of the ArrayList, with a few notable exceptions. The Count property returns the number of items in the collection as usual, but the HashTable collection doesn’t expose a Capacity property. The HashTable collection uses fairly complicated logic to maintain the list of items, and it adjusts its capacity automatically. Fortunately, you need not know how the items are stored in the collection.
To create a HashTable in your code, declare it with the New keyword:
Dim hTable As New HashTable
Code language: PHP (php)
To add an item to the HashTable, use the Add method with the following syntax:
hTable.Add(key, value)
Code language: CSS (css)
value is the item you want to add (it can be any object), and key is a value you supply, which represents the item. This is the value you’ll use later to retrieve the item. If you’re setting up a structure for storing temperatures in various cities, use the city names as keys:
Dim Temperatures As New HashTable
Temperatures.Add("Houston", 81)
Temperatures.Add("Los Angeles", 78)
Code language: PHP (php)
To find out the temperature in Houston, use the following statement:
MsgBox(Temperatures("Houston").ToString)
Code language: JavaScript (javascript)
Notice that you can have duplicate values, but the keys must be unique. If you attempt to use an existing key, an InvalidArgumentException exception will be thrown. To find out whether a specific key or value is already in the collection, use the ContainsKey and ContainsValue methods. The syntax of the two methods is quite similar, and they return True if the specified value is already in use in the collection:
hTable.ContainsKey(object)
hTable.ContainsValue(object)
Code language: CSS (css)
The HashTable collection exposes the Contains method, too, which is identical to the ContainsKey method.
To find out whether a specific key is in use already, use the ContainsKey method, as shown in the following statements, which add a new item to the HashTable only if its key doesn’t exist already:
Dim value As New Rectangle(100, 100, 50, 50)
Dim key As String = "Rect1"
If Not hTable.ContainsKey(key) Then
hTable.Add(key, value)
End If
Code language: PHP (php)
The Values and Keys properties allow you to retrieve all the values and the keys in the HashTable, respectively. Both properties are collections and expose the usual members of a collection. To iterate through the values stored in the HashTable hTable, use the following loop:
Dim itm As Object
For Each itm In hTable.Values
Debug.WriteLine(itm)
Next
Code language: CSS (css)
There is only one method to remove items from a HashTable — the Remove method, which accepts as an argument the key of the item to be removed:
hTable.Remove(key)
Code language: CSS (css)
To extract items from a HashTable, use the CopyTo method. This method copies the items to a one-dimensional array, and its syntax is as follows:
newArray = HTable.CopyTo(arrayName)
You must set up the array that will accept the items beforehand, but you need not supply its dimensions in the declaration. The CopyTo method can throw several different exceptions for various error conditions. The array that accepts the values must be one-dimensional, and there should be enough space in the array for the HashTable’s values. Moreover, the array’s type must be Object because a HashTable stores objects.
Listing 10.5 demonstrates how to scan the keys of a HashTable through the Keys property and then use these keys to access the matching items through the Item property.
Listing 10.5: Iterating a HashTable
Private Function ShowHashTableContents( _
ByVal table As Hashtable) As String
Dim msg As String
Dim element, key As Object
msg = "The HashTable contains " & _
table.Count.tostring & " elements: " & vbCrLf
For Each key In table.keys
element = table.Item(key)
msg = msg & vbCrLf
msg = msg & " Element Type = " & element.GetType.ToString
msg = msg & vbCrLf & " Element Key= " & Key.ToString
msg = msg & " Element Value= " & element.ToString & vbCrLf
Next
Return(msg)
End Sub
Code language: JavaScript (javascript)
To print the contents of a HashTable variable in the Output window, call the ShowHashTableContents() function, passing the name of the HashTable as an argument, and then print the string returned by the function:
Dim HT As New HashTable
{ statements to populate HashTable}
Debug.WriteLine(ShowHashTableContents(HT))
Code language: PHP (php)