Visual Basic .NET - Hash Table

In VB.NET, a HashTable is a collection of key-value pairs where each key is unique. It uses a hash algorithm to compute the index of the elements in the collection, making it very efficient for searching, inserting, and deleting elements.

To use a HashTable, you need to declare an instance of it and specify the data types of the key and the value. 

Dim employees As New Hashtable
employees.Add("John", 123)
employees.Add("Mary", 456)
employees.Add("Bob", 789)

In this example, the keys are the names of the employees, and the values are their ID numbers. To access the values in the HashTable, you can use the key as an index:

Dim johnID As Integer = employees("John")

This will retrieve the value associated with the key "John", which is 123 in this case.

You can also check if a key exists in the HashTable using the ContainsKey method:

If employees.ContainsKey("Mary") Then
    Console.WriteLine("Mary's ID is: " & employees("Mary"))
End If

This will output "Mary's ID is: 456" since the key "Mary" exists in the HashTable.

Other useful methods for working with HashTables include Remove to remove a key-value pair, Clear to remove all elements from the HashTable, and Count to get the number of elements in the HashTable.

employees.Remove("Bob")
employees.Clear()
Dim numEmployees As Integer = employees.Count

In the above example, the Remove method is used to remove the key-value pair with the key "Bob", the Clear method is used to remove all elements from the HashTable, and the Count method is used to get the number of remaining elements in the HashTable.

Creating a Hashtable:

Dim ht As New Hashtable()
ht.Add("key1", "value1")
ht.Add("key2", "value2")
ht.Add("key3", "value3")

Accessing values using keys

Dim value1 As String = ht("key1")
Dim value2 As String = ht("key2")
Dim value3 As String = ht("key3")
' Iterating over all key-value pairs
For Each key As Object In ht.Keys
    Dim value As String = ht(key)
    Console.WriteLine("Key: " & key & ", Value: " & value)
Next

Updating Values:

Dim ht As New Hashtable()
ht.Add("key1", "value1")
ht.Add("key2", "value2")
ht.Add("key3", "value3")
' Updating value for existing key
ht("key1") = "new value"
' Iterating over all key-value pairs
For Each key As Object In ht.Keys
    Dim value As String = ht(key)
    Console.WriteLine("Key: " & key & ", Value: " & value)
Next

Removing Elements:

Dim ht As New Hashtable()
ht.Add("key1", "value1")
ht.Add("key2", "value2")
ht.Add("key3", "value3")
' Removing an element using key
ht.Remove("key2")
' Iterating over all key-value pairs
For Each key As Object In ht.Keys
    Dim value As String = ht(key)
    Console.WriteLine("Key: " & key & ", Value: " & value)
Next

Checking for Existence:

Dim ht As New Hashtable()
ht.Add("key1", "value1")
ht.Add("key2", "value2")
ht.Add("key3", "value3")
' Checking if key exists
Dim exists1 As Boolean = ht.ContainsKey("key1")
Dim exists2 As Boolean = ht.ContainsKey("key4")
Console.WriteLine("Exists1: " & exists1)
Console.WriteLine("Exists2: " & exists2)
' Checking if value exists
Dim exists3 As Boolean = ht.ContainsValue("value1")
Dim exists4 As Boolean = ht.ContainsValue("value4")
Console.WriteLine("Exists3: " & exists3)
Console.WriteLine("Exists4: " & exists4)