First, the hashtable (Hashtable)
In .NET Framework, HashTable is a container provided by the System.Collections namespace for processing and expressing key-value pairs like Key / Value, where KEY is usually available, while Key is case sensitive; VALUE is used for Store values corresponding to Key. The key / value key value in HashTable is an Object type, so HashTable can support any type of Key / Value key value pair.
Second, a simple operation of the hash table
Add a Key / Value key value in the hash table: HashTableObject.Add (key, value);
Remove a Key / Value key value in a hash table: HashTableObject.remove (key);
Remove all elements from the hash table: HashTableObject.clear ();
Judging whether the hash table contains a specific key key: HashTableObject.contains (key);
The following console programs will contain all over the above operations:
Using system;
Using system.collections; // When using HashTable, you must introduce this namespace.
Class hashtable
{
Public static void main ()
{
Hashtable ht = new hashtable (); // Create a HashTable instance
HT.ADD ("e", "e"); // Add Key / Value key value pair
HT.Add ("a", "a");
HT.ADD ("C", "C");
HT.ADD ("B", "B");
String s = (string) HT ["a"];
if (HT.Contains ("E")) // Judging whether the hash table contains a specific key, its return value is TRUE or FALSE
Console.WriteLine ("The e key: exist");
Ht.Remove ("c"); // Remove a key / value key value
Console.writeline (HT ["A"]); // output a here
Ht.clear (); // Remove all elements
Console.Writeline (HT ["A"]); // will not have any output
}
}
Third, traversing a hash table
Traversing the hash table needs to use DictionaryEntry Object, the code is as follows:
For (DictionaryEntry de In HT) // HT is a HashTable instance
{
Console.writeline (de.key); // de.key corresponds to Key / Value key value to Key
Console.writeLine (de.value); // de.key corresponds to the key / value key value to Value
}
Fourth, sort the hash table
Sort the hash table definition here is to rearrange the key to the key / value key value to rearrange the key, but in fact, this definition cannot be implemented, because we can't directly rearrange the key directly, If HashTable is required to provide the output of a certain rule, a variant can be used:
ArrayList akeys = new arraylist (ht.keys); // Don't forget to import system.collections
akeys.sort (); // Sort by alphabetical order
For (String Skey in akeys)
{
Console.write (SKEY ":");
Console.writeline (HT [SKEY]); // Sort Output}