Hashtable

Hashtable


Represents a collection of key/value pairs that are organized based on the hash code of the key.


Implements:

ICloneable 
ICollection 
IDictionary 
IEnumerable 
IObject 
IVersionable 

Public:

Properties:

NameDescription
 Count (get) Gets the number of key/value pairs contained in the Hashtable.  
 IsFixedSize (get) Gets a value indicating whether the Hashtable has a fixed size.  
 IsReadOnly (get) Gets a value indicating whether the Hashtable is read-only.  
 Item (get) Gets the value associated with the specified key.  
 Item (let) Sets the value associated with the specified key.  
 Item (set) Sets the value associated with the specified key.  
 Keys (get) Gets an ICollection containing the keys in the Hashtable.  
 Values (get) Gets an ICollection containing the values in the Hashtable.  

Methods:

NameDescription
 Add Adds an element with the specified key and value into the Hashtable.  
 Clean Removes Deleted buckets from the internal array.  
 Clear Removes all elements from the Hashtable.  
 Clone Creates a shallow copy of the Hashtable.  
 Contains Determines whether the Hashtable contains a specific key.  
 ContainsKey Determines whether the Hashtable contains a specific key.  
 ContainsValue Determines whether the Hashtable contains a specific value.  
 CopyTo Copies the Hashtable elements to a one-dimensional Array instance at the specified index.  
 Equals Returns a boolean indicating if the value and this object instance are the same instance.  
 GetEnumerator Returns an IDictionaryEnumerator that iterates through the Hashtable.  
 GetHashCode Returns a pseudo-unique number identifying this instance.  
 Remove Removes the element with the specified key from the Hashtable.  
 ToString Returns a string representation of this object instance.  

Remarks

The Hashtable is a collection that uses the hash code and equality of a key value to identify a specific value stored. Keys can be primitives, strings, objects, and variant sub-types such as Empty and Null.

The default usage of an object as a key is to use the ObjPtr for the hash code and a reference check for equality. This may not be suitable for a specific application. The objects used a key by a Hashtable should implement the IObject interface and provide more appropriate GetHashCode and Equals methods for the specific application.

The load factor of a Hashtable determines the maximum ratio of elements to buckets. Smaller load factors cause faster average lookup times at the cost of increased memory consumption. The default load factor of 1.0 generally provides the best balance between speed and size. A different load factor can also be specified when the Hashtable is created.

As elements are added to a Hashtable, the actual load factor of the Hashtable increases. When the actual load factor reaches the specified load factor, the number of buckets in the Hashtable is automatically increased to the smallest prime number that is larger than twice the current number of Hashtable buckets.

The For Each statement enumerates each element in the collection. Since each element of the Hashtable is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is DictionaryEntry. For example:

    Dim Entry As DictionaryEntry
    
    For Each Entry In MyHashTable
        ' ...
    Next

Examples

The following example shows how to create, initialize and perform various functions to a Hashtable and how to print out its keys and values.

Public Sub Main()
    ' Create a new hash table.
    Dim OpenWith As New Hashtable

    ' Add some elements to the hash table. There are no
    ' duplicate keys, but some of the values are duplicates.
    OpenWith.Add "txt", "notepad.exe"
    OpenWith.Add "bmp", "paint.exe"
    OpenWith.Add "dib", "paint.exe"
    OpenWith.Add "rtf", "wordpad.exe"

    ' The Add method throws an exception if the new key is
    ' already in the hash table.
    On Error GoTo Catch
    OpenWith.Add "txt", "winword.exe"
    GoTo EndTry
Catch:
    Debug.Print "An element with Key = ""txt"" already exists."
EndTry:

    ' The Item property is the default property, so you
    ' can omit its name when accessing elements.
    Debug.Print CorString.Format("For key = ""rtf"", value = {0}.", OpenWith("rtf"))

    ' The default Item property can be used to change the value
    ' associated with a key.
    OpenWith("rtf") = "winword.exe"
    Debug.Print CorString.Format("For key = ""rtf"", value = {0}.", OpenWith("rtf"))

    ' If a key does not exist, setting the default Item property
    ' for that key adds a new key/value pair.
    OpenWith("doc") = "winword.exe"

    ' ContainsKey can be used to test keys before inserting
    ' them.
    If Not OpenWith.ContainsKey("ht") Then
        OpenWith.Add "ht", "hypertrm.exe"
        Debug.Print CorString.Format("Value added for key = ""ht"": {0}", OpenWith("ht"))
    End If

    ' When you use foreach to enumerate hash table elements,
    ' the elements are retrieved as KeyValuePair objects.
    Dim DE As DictionaryEntry
    
    Debug.Print
    
    For Each DE In OpenWith
        Debug.Print CorString.Format("Key = {0}, Value = {1}", DE.Key, DE.Value)
    Next DE

    ' To get the values alone, use the Values property.
    Dim ValueColl As ICollection
    Set ValueColl = OpenWith.Values

    ' The elements of the ValueCollection are strongly typed
    ' with the type that was specified for hash table values.
    Dim Value As Variant
    
    Debug.Print
    
    For Each Value In ValueColl
        Debug.Print CorString.Format("Value = {0}", Value)
    Next

    ' To get the keys alone, use the Keys property.
    Dim KeyColl As ICollection
    Set KeyColl = OpenWith.Keys

    ' The elements of the KeyCollection are strongly typed
    ' with the type that was specified for hash table keys.
    Dim Key As Variant
    
    Debug.Print
    
    For Each Key In KeyColl
        Debug.Print CorString.Format("Key = {0}", Key)
    Next

    ' Use the Remove method to remove a key/value pair.
    Debug.Print vbLf & "Remove(""doc"")"
    OpenWith.Remove "doc"

    If Not OpenWith.ContainsKey("doc") Then
        Debug.Print "Key ""doc"" is not found."
    End If
End Sub

' This example code produces the following output.
'
'    An element with Key = "txt" already exists.
'    For key = "rtf", value = wordpad.exe.
'    For key = "rtf", value = winword.exe.
'    Value added for key = "ht": hypertrm.exe
'
'    Key = dib, Value = paint.exe
'    Key = ht, Value = hypertrm.exe
'    Key = doc, Value = winword.exe
'    Key = rtf, Value = winword.exe
'    Key = bmp, Value = paint.exe
'    Key = txt, Value = notepad.exe
'
'    Value = Paint.exe
'    Value = hypertrm.exe
'    Value = winword.exe
'    Value = winword.exe
'    Value = Paint.exe
'    Value = notepad.exe
'
'    Key = dib
'    Key = ht
'    Key = doc
'    Key = rtf
'    Key = bmp
'    Key = txt
'
'    Remove ("doc")
'    Key "doc" is not found.

See Also

Project CorLib Overview

Class Hashtable Overview

Constructors

IDictionary

ICollection

IEnumerable

ICloneable

CaseInsensitiveComparer