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 key/value. 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, key/value pairs are of the object type, so Hashtable can support any type of key/value pairs.
Ii. Simple operations on Hash Tables
Add a key/value pair in the hash table: HashtableObject. Add (key, value );
Remove a 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; // This namespace must be introduced when Hashtable is used.
Class hashtable
{
Public static void Main ()
{
Hashtable ht = new Hashtable (); // create a Hashtable instance
Ht. Add ("E", "e"); // Add key/value pairs
Ht. Add ("A", "");
Ht. Add ("C", "c ");
Ht. Add ("B", "B ");
String s = (string) ht ["A"];
If (ht. Contains ("E") // checks 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 key/value pair
Console. WriteLine (ht ["A"]); // output
Ht. Clear (); // remove all elements
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 (DictionaryEntry de in ht) // ht is a Hashtable instance.
{
Console. WriteLine (de. Key); // de. Key corresponds to the key-value Pair
Console. WriteLine (de. Value); // de. Key corresponds to the key/value Key-value Pair Value
}
4. Sort hash tables
The definition of sorting hash tables here is to re-arrange the keys in key/value pairs 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. Source: kwklover's Blog keyword Hashtable, hash table
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 key/value. 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, key/value pairs are of the object type, so Hashtable can support any type of key/value pairs.
Ii. Simple operations on Hash Tables
Add a key/value pair in the hash table: HashtableObject. Add (key, value );
Remove a 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; // This namespace must be introduced when Hashtable is used.
Class hashtable
{
Public static void Main ()
{
Hashtable ht = new Hashtable (); // create a Hashtable instance
Ht. Add ("E", "e"); // Add key/value pairs
Ht. Add ("A", "");
Ht. Add ("C", "c ");
Ht. Add ("B", "B ");
String s = (string) ht ["A"];
If (ht. Contains ("E") // checks 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 key/value pair
Console. WriteLine (ht ["A"]); // output
Ht. Clear (); // remove all elements
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 (DictionaryEntry de in ht) // ht is a Hashtable instance.
{
Console. WriteLine (de. Key); // de. Key corresponds to the key-value Pair
Console. WriteLine (de. Value); // de. Key corresponds to the key/value Key-value Pair Value
}
4. Sort hash tables
The definition of sorting hash tables here is to re-arrange the keys in key/value pairs 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 by letter
For (string skey in akeys)
{
Console. Write (skey + ":");
Console. WriteLine (ht [skey]); // output after sorting
} Keys); // do not forget to import System. Collections
Akeys. Sort (); // Sort by letter
For (string skey in akeys)
{
Console. Write (skey + ":");
Console. WriteLine (ht [skey]); // output after sorting
}