First, what is Hashtable?
The Hashtable class represents a series of key/value pairs that are organized by key-based hash codes. It uses keys to access the elements in the collection.
When you use keys to access elements, a hash table is used, and you can identify a useful key value. Each item in the hash table has a key/value pair. Keys to access items in the collection
Second, the Hashtable property
1. Count gets the number of key-value pairs contained in the Hashtable.
2, Isfixedsize Gets a value that indicates whether the Hashtable has a fixed size.
3. IsReadOnly gets a value that indicates whether the Hashtable is read-only.
4. Item Gets or sets the value associated with the specified key.
5. Keys gets a ICollection that contains the keys in the Hashtable.
6. Values gets a ICollection that contains the values in the Hashtable.
Three, the method of Hashtable
Use in C # requires reference
Using System.Collections;
Using System.Collections.Generic;
These two namespaces
1. Public virtual void Add (object key, object value);
Purpose: Adds an element with the specified key and value to the Hashtable.
2, public virtual void Clear ();
Purpose: Removes all elements from the Hashtable.
3. Public virtual bool ContainsKey (object key);
Purpose: Determines whether the Hashtable contains the specified key.
4. Public virtual bool Containsvalue (object value);
Purpose: Determines whether the Hashtable contains the specified value.
5, public virtual void Remove (object key);
Purpose: Removes the element with the specified key from the Hashtable.
Iv. Application Scenarios for Hashtable
1, some data will be high frequency query
2, the amount of data is very large
3. Query field contains string type
4, data type is not unique
V. Examples of simple usages of Hashtable
Class Program
{
static void Main (string[] args)
{
Create a Hashtable instance
Hashtable ht=new Hashtable ();
Adding keyvalue key-value pairs
Ht. ADD ("A", "1");
Ht. ADD ("B", "2");
Ht. ADD ("C", "3");
Ht. ADD ("D", "4");
Traversing a hash table can only be traversed with foreach, because Hashtable cannot be accessed with an index
Traverse key
foreach (Object item in HT. Keys)
{
Console.WriteLine (string) item);
}
Traverse value
foreach (Object item in HT. Value)
{
Console.WriteLine (string) item);
}
foreach (DictionaryEntry de in HT)
{
Console.WriteLine ("Key--{0}; Value--{1}. ", De. Key, DE. Value);
}
Hash table Sort
ArrayList akeys=new ArrayList (ht. Keys);
Akeys. Sort ();
foreach (string skey in Akeys)
{
Console.WriteLine ("{0, -15} {1, -15}", Skey, Ht[skey]);
}
Determines whether the hash table contains a specific key with a return value of TRUE or False
if (HT. Contains ("A"))
Console.WriteLine (ht["A"]);
Assign a value to the corresponding key
ht["A"] = "Hello";
Remove a KeyValue key-value pair
Ht. Remove ("C");
Traversing a hash table
foreach (DictionaryEntry de in HT)
{
Console.WriteLine ("Key--{0}; Value--{1}. ", De. Key, DE. Value);
}
Remove all elements
Ht. Clear ();
There will be no output here
Console.WriteLine (ht["A"]);
Console.readkey ();
}
}
Public Number: Original
C # Hash Table (Hashtable) Usage notes