Hashtable
1. Brief description of the hash table (hashtable)
In. in net work, hashtable is system. A container provided by the collections namespace is used to process and present key-value pairs similar to key/. The key is usually used for quick search, and the key is case sensitive; store the value corresponding to the key. In hashtable, key/key-value pairs are of the object type, so hashtable can support any type of key/key-value pairs.
Ii. Simple operations on Hash Tables
Add a key/key-value pair to the hash table: hashtableobject. Add (Key ,);
Remove a key/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"]; // obtain the value directly based on the key
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 key/key-Value Pair
Console. writeline (De.); // de. Key corresponds to key/key-Value Pair
}
4. Sort hash tables
The definition of sorting hash tables here is to re-arrange the keys in key/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
// This improves the query speed
Akeys. Sort (); // sort by letter
For (string skey in akeys)
{
Console. Write (skey ":");
Console. writeline (HT [skey]); // output after sorting
}
5. access other objects in the hash table
// Class Object
Public class person
{
Private string firstname;
Private string lastname;
Public Person (string first, string last)
{
Firstname = first;
Lastname = last;
}
Public String fullname
{
Get
{
Return firstname + "" + lastname;
}
}
}
// Access process
Protected void page_load (Object sender, eventargs E)
{
Person Scott = new person ("Scott", "hanselman ");
Person bill = new person ("bill", "evjen ");
Person srini = new person ("Srinivasa", "sivakumar ");
Hashtable peoplehashtable = new hashtable ();
Peoplehashtable. Add ("sh", Scott );
Peoplehashtable. Add ("be", Bill );
Lelehashtable. Add ("SS", srini );
Person found = (person) peoplehashtable ["sh"];
Response. Write (found. fullname + "<br/> ");
Found = (person) peoplehashtable ["be"];
Response. Write (found. fullname + "<br/> ");
Found = (person) peoplehashtable ["sh"];
Response. Write (found. fullname + "<br/> ");
/// Write Method 1:
// Foreach (dictionaryentry de in peoplehashtable)
//{
// Response. Write (De. Key. tostring () + ":" + (person) De. Value). fullname +
// "<Br/> ");
//}
/// Write Method 2:
// Foreach (string s in peoplehashtable. Keys)
//{
// Response. Write (S + "<br/> ");
//}
// Foreach (person P in peoplehashtable. values)
//{
// Response. Write (P. fullname + "<br/> ");
//}
}
6. How to read the key based on value in hashtable
Hashtable T = new hashtable ();
Foreach (dictionaryentry I in T)
{
If (I. value = "value1") return I. Key;
}