I. Introduction
Terms: A Hash table is a data structure that is directly accessed based on the Key value. That is to say, It maps the key value to a location in the table to access records to speed up the search. This ing function is called a hash function, and the array storing records is called a hash function.
Advantages of HashTable tables: HashTable is a container provided by the System. Collections namespace. The key/value in HashTable is of the object type, so HashTable can support any type of key/value pairs.
The advantage of HashTable lies in its indexing method, which is very fast.
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:
123456789101112131415161718192021222324252627282930313233343536373839
Using System; using System. collections; // file when using Hashtable, you must introduce this namespace class hashtable {public static void Main () {Hashtable ht = new Hashtable (); // file to 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) // file to determine whether a hash table Contains a specific key. The return value is true or false Console. writeLine (the E keyexist); 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]); // file 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 (DictionaryEntry de in ht) // fileht is a Hashtable instance {Console. WriteLine (de. Key );