Definition and usage of HashTable in C
- 2020-05-09 19:08:23
- OfStack
1. Hash table (Hashtable) description
In.NET Framework, Hashtable is a container provided by the System.Collections namespace for handling and representing key-value pairs similar to keyvalue, where key is usually used for quick lookup and key is case sensitive; value is used to store values corresponding to key. keyvalue key-value pairs in Hashtable are of type object, so Hashtable can support any type of keyvalue key-value pair.
2. Simple operation of hash table
Add 1 keyvalue key-value pair to the hash table: HashtableObject.Add (key, value);
Remove an keyvalue key value pair from the hash table: HashtableObject.Remove (key);
Remove all elements from the hash table: HashtableObject.Clear ();
Determine whether the hash table contains a specific key key: HashtableObject.Contains (key);
The following console program will contain all of the above actions:
3. Traverse the hash table
To traverse the hash table, DictionaryEntry Object is required. The code is as follows:
4. Sort the hash table
The definition of sorting hash table here is to rearrange key in keyvalue key value pair according to 1 definite rule, but actually this definition cannot be realized, because we cannot rearrange key directly in Hashtable. If Hashtable is required to provide some rule output, we can adopt a flexible method:
In.NET Framework, Hashtable is a container provided by the System.Collections namespace for handling and representing key-value pairs similar to keyvalue, where key is usually used for quick lookup and key is case sensitive; value is used to store values corresponding to key. keyvalue key-value pairs in Hashtable are of type object, so Hashtable can support any type of keyvalue key-value pair.
2. Simple operation of hash table
Add 1 keyvalue key-value pair to the hash table: HashtableObject.Add (key, value);
Remove an keyvalue key value pair from the hash table: HashtableObject.Remove (key);
Remove all elements from the hash table: HashtableObject.Clear ();
Determine whether the hash table contains a specific key key: HashtableObject.Contains (key);
The following console program will contain all of the above actions:
using System;
using System.Collections; file use Hashtable , you must introduce this namespace
class hashtable
{
public static void Main()
{
Hashtable ht=new Hashtable(); file create 1 a Hashtable The instance
ht.Add(E,e); add keyvalue 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 Determines whether the hash table contains a particular key , The return value is true or false
Console.WriteLine(the E keyexist);
ht.Remove(C); remove 1 a keyvalue Key/value pair
Console.WriteLine(ht[A]); Here is the output a
ht.Clear(); Remove all elements
Console.WriteLine(ht[A]); file There will be no output here
}
}
3. Traverse the hash table
To traverse the hash table, DictionaryEntry Object is required. The code is as follows:
for(DictionaryEntry de in ht) fileht for 1 a Hashtable The instance
{
Console.WriteLine(de.Key);de.Key Corresponding to the keyvalue Key/value pair key
Console.WriteLine(de.Value);de.Key Corresponding to the keyvalue Key/value pair value
}
4. Sort the hash table
The definition of sorting hash table here is to rearrange key in keyvalue key value pair according to 1 definite rule, but actually this definition cannot be realized, because we cannot rearrange key directly in Hashtable. If Hashtable is required to provide some rule output, we can adopt a flexible method:
ArrayList akeys=new ArrayList(ht.Keys); file Don't forget to import System.Collections
akeys.Sort(); file Sort them alphabetically
for(string skey in akeys)
{
Console.Write(skey + );
Console.WriteLine(ht[skey]); Sorted output
}