1. hashtable Overview
Elements in the hashtable set exist as key/value. Key is used for quick search; value is used to store the value corresponding to the key. It is worth noting that both key and value are of the object type.
Ii. hashtable operations
Hashtable operations include:
1) Add a set Element
2) retrieve collection Elements
3) Traversal
4) sorting set Elements
5) modify the collection Element
6) Delete collection Elements
The following simple console program implements the above operations (C #)
Namespace hashtableop
{
Using system;
Using system. collections;
Class sample
{
Static void main (string [] ARGs)
{
// Create a hashtable set
Hashtable ht = new hashtable ();
// Add a key/value pair to the set
Ht. Add ("1", "apple ");
Ht. Add ("2", "orange ");
Ht. Add ("3", "strawberry ");
Ht. Add ("0", "Fruit ");
// Find the value through the key
Console. writeline ("Find value by key ");
String v = (string) HT ["1"];
Console. writeline (v );
// Traverse the set
Console. writeline ("/n traversal set ");
Foreach (dictionaryentry de in HT)
Console. writeline ("Key =/" {0}/"/tvalue =/" {1}/"", de. key. tostring (), de. value. tostring ());
// Sort the set
Console. writeline ("/n sorting sets ");
Arraylist akeys = new arraylist (HT. Keys );
Akeys. Sort ();
Foreach (string key in akeys)
Console. writeline ("Key =/" {0}/"/tvalue =/" {1}/"", key, HT [Key]. tostring ());
// Modify the value of the specified key
HT ["0"] = "animal ";
HT ["1"] = "monkey ";
HT ["2"] = "horse ";
HT ["3"] = "donkey ";
Console. writeline ("/n traversing the modified set ");
Foreach (dictionaryentry de in HT)
Console. writeline ("Key =/" {0}/"/tvalue =/" {1}/"", de. key. tostring (), de. value. tostring ());
// Delete a set Element
Ht. Remove ("2 ");
Ht. Remove ("1 ");
Console. writeline ("/n traverse the collection after deleting elements ");
Foreach (dictionaryentry de in HT)
Console. writeline ("Key =/" {0}/"/tvalue =/" {1}/"", de. key. tostring (), de. value. tostring ());
// Delete the collection Element
Ht. Clear ();
}
}
}
1. Basic concepts of collection
In programs, collections are commonly used to organize elements that have an indefinite number and do not need to explicitly express the Successor Relationship ). It can be a metaphor to describe a set: A set is similar to a bag. This bag is used to hold items (set elements), and the items in the bag are unordered. The core meaning of the Set described in regular terms is the same as that expressed in the previous metaphor. A collection is a container object used to store collection elements ). In addition, the Set provides various methods for accessing, retrieving, and traversing the set elements.
Generally, a set provides the following operations on the set elements:
1) Add adds an element to the collection container;
2) remove an element from the collection container;
3) Clear clears the elements in the collection container;
4) retrieve a specific set element from the collection container. You can retrieve the set element based on the key of the Set element or the lower mark (INDEX) of the Set element;
5) traverse the collection elements. Traversal is to quickly view all the collection elements in the collection container.
Ii. hashtable
Hashtable is a collection container. The collection element in this container is a bit special. A collection element consists of two objects: one object is the key, and the other object is the value, forming a so-called key/value pair. As defined by msdn, the element type in hashtable is dictionaryentry. Dictionaryentry is an object type with key/value pairs.
Key is used to quickly find (retrieve) hashtable set elements, and value is used to store user data.
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"];
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
Akeys. Sort (); // sort by letter
For (string skey in akeys)
{
Console. Write (skey ":");
Console. writeline (HT [skey]); // output after sorting
}