This article describes the usage of hash tables (HashTable) in C#. Share it for your reference, as follows:
1. A brief description of HashTable
In the .NET Framework, Hashtable is a container provided by the namespace to process and represent key-value pairs similar to keyvalues, where keys can usually be used for quick searches, while keys are case-sensitive; value is used to store values corresponding to keys. The keyvalue key-value pairs in Hashtable are all object types, so Hashtable can support any type of keyvalue key-value pairs.
2. Under what circumstances do you use hash tables?
(1) Some data will be queried at high frequency
(2) Large amount of data
(3) The query field contains string type
(4) The data type is not unique
3. How to use hash table
The namespace that hash table needs to be used
using ; using ;
Basic operations of hash table:
//Add a keyvalue key-value pair:(key,value); //Remove a keyvalue pair:(key); //Remove all elements:(); // Determine whether a specific key is included:(key);
Console program example:
using System; using ; // When a file uses Hashtable, this namespace must be introducedclass Program { public static void Main() { Hashtable ht = new Hashtable(); //Create a Hashtable instance ("Beijing", "Imperial Capital"); //Add keyvalue pair ("Shanghai", "Demon City"); ("Guangzhou", "provincial capital"); ("Shenzhen", "Special Zone"); string capital = (string)ht["Beijing"]; (("Shanghai")); //Judge whether the hash table contains a specific key, and its return value is true or false ("Shenzhen"); //Remove a keyvalue pair (); //Remove all elements } }
Examples of using multiple data types in hash tables:
using System; using ; class Program { static Hashtable GetHashtable() { Hashtable hashtable = new Hashtable(); ("name", "Xiaoli"); ("age", 22); return hashtable; } static void Main() { Hashtable hashtable = GetHashtable(); string name = (string)hashtable["name"]; (name); int age = (int)hashtable["age"]; (age); } }
When getting data in the hash table, if the type declaration is incorrect, an InvalidCastException error will occur. Using as-statements can avoid this error.
using System; using ; using ; class Program { static void Main() { Hashtable hashtable = new Hashtable(); (100, "Xi'an"); // Can be converted successfully string value = hashtable[100] as string; if (value != null) { (value); } // The conversion failed, the obtained value is null, but no error is thrown. StreamReader reader = hashtable[100] as StreamReader; if (reader == null) { ("Xi'an is not a StreamReader type"); } // You can also directly obtain the object value and then make a judgment object value2 = hashtable[100]; if (value2 is string) { ("This is a string type: "); (value2); } } }
4. Traverse the hash table
DictionaryEntry Object is required to traverse the hash table, the code is as follows:
for(DictionaryEntry de in ht) //ht is a Hashtable instance{ (); // Corresponding to keyvalue key value pair (); // Corresponding to the keyvalue key value pair value}
Traversal keys
foreach (int key in ) { (key); }
Traversal value
foreach (string value in ) { (value); }
5. Sort the hash table
Rearrange the hash table by key value:
ArrayList akeys=new ArrayList(); (); //Sort in alphabetical orderforeach(string key in akeys) { (key + ": " + ht[key]); //Output after sorting}
6. The efficiency of hash table
The hash table under (Hashtable) and the dictionary under (Dictionary) can be used as a lookup table. Let's compare the execution efficiency of the two.
Stopwatch sw = new Stopwatch(); Hashtable hashtable = new Hashtable(); Dictionary<string, int> dictionary = new Dictionary<string, int>(); int countNum = 1000000; (); for (int i = 0; i < countNum; i++) { ((), i); } (); (); //Output: 744(); for (int i = 0; i < countNum; i++) { ((), i); } (); (); //Output: 489(); for (int i = 0; i < countNum; i++) { (()); } (); (); //Output: 245(); for (int i = 0; i < countNum; i++) { (()); } (); (); //Output: 192
From this we can see that Hashtable is fast when adding data. Dictionary is fast when calling data frequently.
Conclusion: Dictionary<K,V> is generic, and when K or V is a value type, its speed is much higher than Hashtable.
For more information about C# related content, please check out the topic of this site:Summary of C# traversal algorithm and skills》、《Summary of thread usage techniques for C# programming》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial》
I hope this article will be helpful to everyone's C# programming.