1. A brief description of the hashtable
In the .NET Framework, Hashtable is a container provided by the namespace to process and represent key-value pairs similar to keyvalues, where keys can usually be used for quick searches, while keys are case-sensitive; value is used to store values corresponding to keys. The keyvalue key-value pairs in Hashtable are all object types, so Hashtable can support any type of keyvalue key-value pairs.
2. Simple operation of hash table
Add a keyvalue key-value pair in the hash table: (key,value);
Remove a keyvalue key-value pair in the hash table: (key);
Remove all elements from the hash table: ();
Determine whether the hash table contains a specific key: (key);
The following console program will contain all the above operations:
using System;
using ; when file uses Hashtable, this namespace must be introduced
class hashtable
{
public static void Main()
{
Hashtable ht=new Hashtable(); file creates a Hashtable instance
(E,e);Add keyvalue pair
(A,a);
(C,c);
(B,b);
string s=(string)ht[A];
if((E)) file determines whether the hash table contains a specific key, and its return value is true or false
(the E keyexist);
(C); Remove a keyvalue key-value pair
(ht[A]); output a here
();Remove all elements
(ht[A]); file will not have any output here
}
}
Three, traverse the hash table
DictionaryEntry Object is required to traverse the hash table, and the code is as follows:
for(DictionaryEntry de in ht) fileht is a Hashtable instance
{
(); corresponds to keyvalue key value pair key
(); corresponds to the keyvalue key-value pair value
}
4. Sort the hash table
The definition of sorting the hash table here is to rearrange the keys in the keyvalue key value pair according to certain rules, but in fact this definition cannot be implemented because we cannot directly rearrange the keys in Hashtable. If Hashtable needs to provide some rule output, a workaround can be adopted:
ArrayList akeys=new ArrayList(); file Don't forget to import
(); file sorted alphabetical order
for(string skey in akeys)
{
(skey + );
(ht[skey]); output after sorting
}
In the .NET Framework, Hashtable is a container provided by the namespace to process and represent key-value pairs similar to keyvalues, where keys can usually be used for quick searches, while keys are case-sensitive; value is used to store values corresponding to keys. The keyvalue key-value pairs in Hashtable are all object types, so Hashtable can support any type of keyvalue key-value pairs.
2. Simple operation of hash table
Add a keyvalue key-value pair in the hash table: (key,value);
Remove a keyvalue key-value pair in the hash table: (key);
Remove all elements from the hash table: ();
Determine whether the hash table contains a specific key: (key);
The following console program will contain all the above operations:
Copy the codeThe code is as follows:
using System;
using ; when file uses Hashtable, this namespace must be introduced
class hashtable
{
public static void Main()
{
Hashtable ht=new Hashtable(); file creates a Hashtable instance
(E,e);Add keyvalue pair
(A,a);
(C,c);
(B,b);
string s=(string)ht[A];
if((E)) file determines whether the hash table contains a specific key, and its return value is true or false
(the E keyexist);
(C); Remove a keyvalue key-value pair
(ht[A]); output a here
();Remove all elements
(ht[A]); file will not have any output here
}
}
Three, traverse the hash table
DictionaryEntry Object is required to traverse the hash table, and the code is as follows:
Copy the codeThe code is as follows:
for(DictionaryEntry de in ht) fileht is a Hashtable instance
{
(); corresponds to keyvalue key value pair key
(); corresponds to the keyvalue key-value pair value
}
4. Sort the hash table
The definition of sorting the hash table here is to rearrange the keys in the keyvalue key value pair according to certain rules, but in fact this definition cannot be implemented because we cannot directly rearrange the keys in Hashtable. If Hashtable needs to provide some rule output, a workaround can be adopted:
Copy the codeThe code is as follows:
ArrayList akeys=new ArrayList(); file Don't forget to import
(); file sorted alphabetical order
for(string skey in akeys)
{
(skey + );
(ht[skey]); output after sorting
}