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 ConsoleProgramAll of the above operations will be included:
Using system;
Using system. collections; this namespace must be introduced when file uses hashtable.
Class hashtable
{
Public static void main ()
{
Hashtable ht = new hashtable (); file to create a hashtable instance
Ht. Add (E, E); add a keyValue pair
Ht. Add (A, );
Ht. Add (C, C );
Ht. Add (B, B );
String S = (string) HT [a];
If (HT. Contains (E) file determines whether the 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
Console. writeline (HT [a]); Output
Ht. Clear (); remove all elements
Console. writeline (HT [a]); file will not have any output here
}
}
3. traverse the hash table
Dictionaryentry object is required to traverse a hash table,CodeAs follows:
For (dictionaryentry de in HT) fileht is a hashtable instance.
{
Console. writeline (De. Key); De. Key corresponds to the key
Console. writeline (De. Value); De. Key corresponds to the 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); file do not forget to import system. Collections
Akeys. Sort (); files are sorted alphabetically.
For (string skey in akeys)
{
Console. Write (skey + );
Console. writeline (HT [skey]); output after sorting
}
AboveArticleSource: http://www.cnblogs.com/liuwenjun830/archive/2006/07/28/462182.html