1. Brief description of the hash table (hashtable)
In. in the. NET Framework, hashtable is system. A container provided by the collections namespace is used to process and present key-value pairs similar to the keyValue. The key is usually used for quick search, and the key is case sensitive; value is used to store the value corresponding to the key. In hashtable, keyValue pairs are of the object type, so hashtable can support any type of keyValue pairs.
Ii. Simple operations on Hash Tables
Add a keyValue key-value pair in the hash table: hashtableobject. Add (Key, value );
Remove a keyValue key-value pair in the hash table: hashtableobject. Remove (key );
Remove all elements from the hash table: hashtableobject. Clear ();
Determine whether the hash table contains the specified key: hashtableobject. Contains (key );
The following console contains all the preceding 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 the key-Value Pair ht. add ("A", "A"); ht. add ("C", "C"); ht. add ("B", "B"); string S = (string) HT ["A"]; If (HT. contains ("e") // determines whether the hash table contains a specific key. The return value is true or false console. writeline ("the e key exist"); ht. remove ("C"); // remove a keyValue pair from the console. writeline (HT ["A"]); // output a HT here. clear (); // remove all elements from the console. writeline (HT ["A"]); // there will be no output here }}
3. traverse the hash table
Dictionaryentry object is required to traverse a hash table. The Code is as follows:
For (keyvaluepair de in HT) // HT is a hashtable instance {console. writeline (de. key); // de. key corresponds to the key-Value Pair key console. writeline (de. value); // de. key corresponds to keyValue key-Value Pair value}
4. Sort hash tables
The definition of sorting hash tables here is to re-arrange the keys in the key-Value Pair according to certain rules, but in fact this definition cannot be implemented, because we cannot re-arrange keys directly in hashtable, if hashtable needs to provide output of certain rules, we can adopt a work und:
Arraylist akeys = new arraylist (HT. keys); // do not forget to import system. collections akeys. sort (); // sort in alphabetical order for (string skey in akeys) {console. write (skey + ":"); console. writeline (HT [skey]); output after sorting}