The usage of hash table (HashTable) in C #
1. Hash table (HashTable) Overview
In the. NET framework, Hashtable is a container provided by the System.Collections namespace that handles and behaves like KeyValue key-value pairs, where key is typically used for quick lookups, while key is case-sensitive ; value is used to store the value corresponding to key. KeyValue Key-value pairs in Hashtable are all object types, so hashtable can support any type of KeyValue key-value pair.
2. When to use the hash table
(1) Some data will be queried by high frequency
(2) Large amount of data
(3) Query field contains string type
(4) data type not unique
3. How to use the hash table
Hash table needs to be used by namespace
Using System.Collections; Using System.Collections.Generic;
Basic operation of the hash table:
Add a KeyValue key value pair:hashtableobject.add (key,value); Remove a KeyValue key value pair:hashtableobject.remove (key); // determine if a specific key key:HashtableObject.Contains (key) is included;
Console program Examples:
UsingSystem;Using System.Collections;//When file uses Hashtable, this namespace must be introducedClassprogram{PublicStaticvoidMain () {Hashtable HT =New Hashtable ();//Create a Hashtable instance ht. ADD ("Beijing","Royal Park");//Add the KeyValue key value to HT. ADD ("Shanghai","Devil's"); Ht. ADD ("Guangzhou","Capital"); Ht. ADD ("Shenzhen"," special ); string capital = (string) ht[ " Beijing ]; Console.WriteLine (HT. Contains ( " Shanghai // Determines whether the hash table contains a specific key with a return value of TRUE or false ht. Remove ( " Shenzhen "); // remove a keyvalue key-value pair ht. Clear (); // remove all elements }}
Examples of using multiple data types in a hash table:
UsingSystem;UsingSystem.Collections;Classprogram{StaticHashtable gethashtable () {Hashtable Hashtable =NewHashtable (); Hashtable. ADD ("Name","Xiaoli"); Hashtable. ADD ("Age22); return Hashtable;} static void Main () { Hashtable Hashtable = gethashtable (); string name = (string) Hashtable[ " name " int age = (int) hashtable[ " age ]; Console.WriteLine (age); }}
When the data in the hash table is obtained, a InvalidCastException error occurs if the type declaration is not correct. Use as-statements to avoid this error.
UsingSystem;UsingSystem.Collections;UsingSystem.IO;Classprogram{StaticvoidMain () {Hashtable Hashtable =NewHashtable (); Hashtable. ADD (100,"Xi'an");//Can convert successString value = hashtable[100]AsString;if (Value! =Null) {Console.WriteLine (value);}//The conversion failed with a value of NULL, but no error is thrown. StreamReader reader = hashtable[100]As StreamReader; if (reader = = null " Xi ' An is not StreamReader type "); } // can also get the object value directly, and then make a judgment object value2 = Hashtable[100 if (value2 is string< Span style= "color: #000000;" ) {Console.Write ( " This is a string type: "); Console.WriteLine (value2); } }}
4. Traversing a hash table
Traversing a hash table requires the use of DictionaryEntry Object, the code is as follows:
HT is a Hashtable instance { Console.WriteLine (DE. Key); //// de. Key corresponds to KeyValue value}
Traverse key
foreach (in Hashtable. Keys) { Console.WriteLine (key);}
Traversing values
foreach (in Hashtable. Values) { Console.WriteLine (value);}
5. sort the hash table
The practice of rearranging hash tables by key value:
ArrayList akeys=New// alphabetical sort foreach (in// sort after output}
6. The efficiency of a hash table
The hash table under System.Collections (Hashtable) and the dictionary under System.Collections.Generic (Dictionary) are available as lookup tables, comparing the efficiency of their execution.
Stopwatch SW =NewStopwatch (); Hashtable Hashtable =NewHashtable ();D ictionary<Stringint> dictionary =New dictionary<StringInt>();int countnum =1000000; SW. Start ();for (int i =0; i < Countnum; i++) {Hashtable. ADD (i.ToString (), i);} Sw. Stop (); Console.WriteLine (SW. Elapsedmilliseconds);//Output: 744Sw. Restart ();for (int i =0; i < Countnum; i++) {dictionary. ADD (i.ToString (), i);} Sw. Stop (); Console.WriteLine (SW. Elapsedmilliseconds);output: 489SW. Restart (); For (int i = 0; i < countnum; i++) {hashtable. ContainsKey (i.ToString ());} Sw. Stop (); Console.WriteLine (SW. Elapsedmilliseconds); // output: 245SW. Restart (); For (int i = 0; i < countnum; i++) {dictionary. ContainsKey (i.ToString ());} Sw. Stop (); Console.WriteLine (SW. Elapsedmilliseconds); // output: 192
This article refers to other people's
The use of Hashtable