VB.Net Tutorial/Collections/Hashtable

Материал из VB Эксперт
Перейти к: навигация, поиск

Add value pair to Hashtable and get value back by key

<source lang="vbnet">Imports System Imports System.Collections Class Test

 Shared Sub Main()
   Dim myTable As New Hashtable()
   myTable.Item("A") = 1
   myTable.Item("B") = 44
   myTable.Item("C") = 43
   myTable.Item("D") = 33
   myTable.Item("E") = 39
   Dim Country As String = "C"
   Dim Code As Integer = myTable.Item(Country)
   Console.WriteLine(Code)
 End Sub

End Class</source>

43

ContainsKey

<source lang="vbnet">Imports System.Collections public class Test

  public Shared Sub Main
       Dim myTable As New Hashtable
       Dim objKey As Object
       Dim objValue As Object
       "Populate the hash table.
       myTable.Add("H", 1)
       myTable.Add("L", 2)
       "Check before inserting another value.
       If Not myTable.ContainsKey("A") Then
           myTable.Add("A", 78)
       End If
       "Remove a value.
       myTable.Remove("L")
       "Display the hash table values.
       Console.WriteLine("The hash table contains " & myTable.Count & " elements.")
       For Each objKey In myTable.Keys
           objValue = myTable.Item(objKey)
           Console.WriteLine("ELEMENT KEY: " & objKey.ToString & vbCrLf & _
                  "ELEMENT VALUE: " & objValue.ToString)
       Next objKey
  End Sub

End class</source>

The hash table contains 2 elements.
ELEMENT KEY: A
ELEMENT VALUE: 78
ELEMENT KEY: H
ELEMENT VALUE: 1

Find value and key in a Hashtable

<source lang="vbnet">Imports System.Collections Public Module Hasher

   Dim Tablet As Hashtable = New Hashtable()
   Sub Main()
       AddToTable()
       FindItem("AAA")
       FindKey("BBB")
   End Sub
   Public Sub AddToTable()
       Tablet.Add("333", "WWWW")
       Tablet.Add("444", "AAA")
       Tablet.Add("BBB", "I386")
   End Sub
   Public Sub FindItem(ByVal item As String)
       If Tablet.ContainsValue(item) Then
           Console.WriteLine("Found {0} in the hashtable.", item)
       End If
   End Sub
   Public Sub FindKey(ByVal key As String)
       If Tablet.ContainsKey(key) Then
           Console.WriteLine("Found {0} in the list.", key)
       End If
   End Sub

End Module</source>

Found AAA in the hashtable.
Found BBB in the list.

Get Enumerator from Hashtable

<source lang="vbnet">Imports System.Collections public class Test

  public Shared Sub Main
       Dim Hash As New Hashtable()
       Hash.Add("5", "A")
       Hash.Add("6", "B")
       Hash.Add("7", "C")
   
       Dim Enumerator As IDictionaryEnumerator = Hash.GetEnumerator
   
       While (Enumerator.MoveNext())
         Console.WriteLine(Enumerator.Key.ToString + "=" + Enumerator.Value.ToString)
       End While
  End Sub

End class</source>

5=A
6=B
7=C

Retrieve all keys, sort and iterate

<source lang="vbnet">Option Strict On Imports System.Collections Public Module modMain

  Public Sub Main()
     Dim airports As New Hashtable
     airports.Add("S", "SSS")
     airports.Add("X", "XXX")
     airports.Add("D", "DDD")
     airports.Add("R", "RRR")
     
     Dim keys As ICollection = airports.Keys
     Dim keysArray(airports.Count - 1) As String
     keys.CopyTo(keysArray, 0)
     Array.Sort(keysArray)
     For Each key As String in KeysArray
        Console.WriteLine("{0} is {1}", key, airports(key))
     Next
  End Sub

End Module</source>

D is DDD
R is RRR
S is SSS
X is XXX

Retrieve all values, sort and iterate

<source lang="vbnet">Option Strict On Imports System.Collections Public Module modMain

  Public Sub Main()
     Dim airports As New Hashtable
     airports.Add("S", "SSS")
     airports.Add("X", "XXX")
     airports.Add("D", "DDD")
     airports.Add("R", "RRR")
     
     Dim values As ICollection = airports.Values
     Dim valuesArray(airports.Count - 1) As String
     values.CopyTo(valuesArray, 0)
     Array.Sort(valuesArray)
     Console.WriteLine()
     For Each value As String in valuesArray
        Console.WriteLine(value)
     Next
  End Sub

End Module</source>

DDD
RRR
SSS
XXX

Retrieve single value from Hashtable by key

<source lang="vbnet">Option Strict On Imports System.Collections Public Module modMain

  Public Sub Main()
     Dim airports As New Hashtable
     airports.Add("JFK", "John F. Kennedy, New York")
     airports.Add("LAX", "Los Angeles International, Los Angeles")
     airports.Add("ORD", "O"Hare International, Chicago")
     airports.Add("LHR", "Heathrow Airport, London")
     Console.WriteLine(airports.Item("JFK"))
  End Sub

End Module</source>

John F. Kennedy, New York

Use For Each to loop through all keys in Hashtable

<source lang="vbnet">Imports System.Collections public class Test

  public Shared Sub Main
       Dim myTable As New Hashtable
       Dim objKey As Object
       Dim objValue As Object
       "Populate the hash table.
       myTable.Add("H", 1)
       myTable.Add("L", 2)
       "Check before inserting another value.
       If Not myTable.ContainsKey("A") Then
           myTable.Add("A", 78)
       End If
       "Remove a value.
       myTable.Remove("L")
       "Display the hash table values.
       Console.WriteLine("The hash table contains " & myTable.Count & " elements.")
       For Each objKey In myTable.Keys
           objValue = myTable.Item(objKey)
           Console.WriteLine("ELEMENT KEY: " & objKey.ToString & vbCrLf & _
                  "ELEMENT VALUE: " & objValue.ToString)
       Next objKey
  End Sub

End class</source>

The hash table contains 2 elements.
ELEMENT KEY: A
ELEMENT VALUE: 78
ELEMENT KEY: H
ELEMENT VALUE: 1