Hashtable - Better than VB Collection? (sometimes)
In this tutorial, a quick overview of the Hashtable class
is presented. Simple operations are demonstrated to show the basics of adding, removing and
iterating over the items.
What is a hashtable? A hashtable is a list of key-value pairs stored in buckets that are primarily keyed based on a
number that can be generated from the supplied key. The number is used as a starting index
of sorts to quickly locate an associated item within the list. The number is refered to
has a hashcode. It is not a unique number, but is differentiating enough to cut out most
of the other keys in the list, allowing for quicker searches for the specific key.
How is the Hashtable any better than the VB Collection? The collection uses keys aswell, or
even no keys if desired. In situations where no key is needed, then Hashtable is not the
appropriate collection class. However, in situations where the keys themselves need to be
retrieved, or an item with an existing key needs to be changed, then the Hashtable works
beyond that of the VB Collection.
Items in the Hashtable are stored in Buckets to keep the key-value pairs together.
Once the number of occupied buckets reaches a specific threshold, then the table is expanded
to accomodate more key-value pairs. The key can be any datatype other than a User-Defined type. This
is different from the VB Collection in that the Hashtable can use numbers and even objects as keys. Even
though two keys may generate the same hashcode, the keys themselves must still be unique.
In this tutorial, a list of employees is managed with their employee ID as the key.
Creating a new Hashtable is like creating any other object.
Dim employees As Hashtable
Set employees = New Hashtable
This will create a default Hashtable with a capacity of approximately 32 buckets. We say
approximately because the number of buckets will actually be the next prime number. This
helps in the key's hashcode to be more evenly distributed.
We have two existing employees, so we will use Add to add them to the table directly.
employees.Add 1234, "Kelley"
employees.Add 2333, "Scott"
Adding items to the Hashtable is similar to that of the VB Collection. Other than the
reversal of the key-value arguments, the main difference is that numeric values have
been used as keys for the employees. Other datetypes can be used as keys at the
same time. All of the keys do not need to be of the same datatype at once.
One of the employee names was spelled incorrectly. Kelley should be Kelly. To change
an existing value in the Hashtable you can simply access the value using the key as
the index, similar to an array.
employees(1234) = "Kelly"
As demonstrated, you can easily set the value for an existing key-value pair. To perform the same
function using a VB Collection, the value would need to be removed, then re-added.
We have a new hire and want to check if an ID is already in use. Since we are using
the employee ID as the key, we can check the Hashtable for that key using ContainsKey
Debug.Print "Employee ID in use: "; employees.ContainsKey(6789)
' output
' Employee ID in use: False
The table reports that no such key is present, so we will use this ID.
Even though the ID does not exist in the table, we can add the new key-value pair
directly without using the Add method.
employees(6789) = "John"
This works just as before, execpt this time the key-value pair is added to the table
because it did not previously exist.
Now that we have our new hire added, lets see if we have enough employees to
work on the projects. We can check the number of employees by using the Count property.
Debug.Print "Number of Employees: "; employees.Count
' output
' Number of Employees: 3
Now that we have enough employees, we can iterate through them to find out everything we know.
When iterating though a Hashtable using For..Each, the returned item is a DictionaryEntry. This
object contains both the key and value for an item in the table.
Dim employee As DictionaryEntry
For Each employee In employees
Debug.Print "ID: "; employee.Key; ", Name: "; employee.Value
Next employee
' output
' ID: 1234 , Name: Kelly
' ID: 6789 , Name: John
' ID: 2333 , Name: Scott
Iterating over a Hashtable is just as easy as a VB Collection. The only difference is
that a DictionaryEntry is returned for each iteration containing both the key and value.
The employees are not in the same order as they were added. The Hashtable does not maintain
any order among the key-value pairs. The key determines where in the list the pair will
be added.
What if we just want to iterate over the keys in the Hashtable? That can be accomplished
by iterating over the Keys property of the Hashtable.
Dim ID as Variant
For Each ID In employees.Keys
Debug.Print "ID: "; ID
Next ID
' output
' ID: 1234
' ID: 6789
' ID: 2333
The order of the keys is the same as when iterating over the key-pair of the Hashtable.
We can do the same with the values in the Hashtable and iterate just over them
using the Values property.
Dim Name as Variant
For Each Name In employees.Values
Debug.Print "Name: "; Name
Next Name
' output
' Name: Kelly
' Name: John
' Name: Scott
Someone has been bad and was fired. Removing the employee from the table is the
same as a VB Collection. We can remove a key-value pair using Remove and passing
in the specific key.
employees.Remove 1234
Now that someone has been fired, we can check the current roster again.
For Each employee In employees
Debug.Print "ID: "; employee.Key; ", Name: "; employee.Value
Next employee
' output
' ID: 6789 , Name: John
' ID: 2333 , Name: Scott
The correct employee was indeed fired.
As you can see, the Hashtable has advantages over the VB Collection class. It allows
for access to the keys used to search for values, and easily modify values without
the need for special code. Various forms of iterating over the items is supported to
bring a wide range of flexability to using a Hashtable.
This tutorial has shown some of the basic functionality of the Hashtable. There
are many more methods and properties to be explored. You can read about the
Hashtable in the Docs section.
I hope this has been a helpful quickstart tutorial
|