C # Hash table hashtable and dictionary table dictionary comparison.

Source: Internet
Author: User

a Hashtable and Dictionary <k, v> type

1): Single-threaded program recommended the use of Dictionary, has a generic advantage, and read faster, the capacity to use more fully.
2): Multithreaded programs recommend the use of Hashtable, the default Hashtable allows single-threaded write, multithreaded read, to Hashtable further call Synchronized () method can obtain a completely thread-safe type. But dictionary is not thread safe, must use lock statement to protect, the efficiency is greatly reduced.
3): Dictionary has the characteristics of the data in the order of insertion (note: But when the call remove () delete the node after the order is disrupted), so in the need to reflect the order of the situation in the use of Dictionary can be a certain convenience.

When you use a hash table to save a collection element (a key/value pair), you first automatically compute the hash code based on the key to determine where the element is saved, and then put the value of the element in the bucket to which the corresponding position points. When looking, the hash code corresponding to the key is searched again through a specific bucket, which greatly reduces the number of times the comparison is made to find an element.

The Key/value in Hashtable is of type object and consists of buckets that contain the elements of the collection. A bucket is a virtual subgroup of elements in a Hashtable, which makes searching and retrieving more convenient than searching and retrieving in most collections. Each bucket is associated with a hash code that is generated using a hash function and is based on the key of that element. The advantage of Hashtable is that it is indexed in a very fast way. Accessing elements in any type of key is faster than other collections, especially when the volume of data is particularly large.

Hashtable applications include: Do object caching, tree recursive algorithm substitution, and various need to enhance the efficiency of the occasion.


Second, hash table Hashtabl

HasTable is the implementation of a hash table, which can be based on key words, the type of the key is object, and the type of value is object.

Adds a Key/value key value pair to the hash table: Hashtableobject.add (Key,value);

Removes a Key/value key value pair in a hash table: Hashtableobject.remove (key);

Remove all elements from the hash table: Hashtableobject.clear ();

Determines whether a hash table contains a specific key key:HashtableObject.Contains (key);

two ways to traverse a Hashtable object:

Because each element of Hashtable is a key/value pair, the element type is neither the type of the key nor the type of the value, but the dictionaryentry type.

Hashtable Sample Code 
<pre name= "code" class= "CSharp" >code highlighting by produced Actipro (Codehighlighter). codehighlighter.com/-->//method One
foreach (System.Collections.DictionaryEntry de in myhashtable)
{
    // Note that the default type stored in Hasttable is object, and conversion is required to output
    Console.WriteLine (DE. Key.tostring ());
    Console.WriteLine (DE. Value.tostring ());
}


Method two
System.Collections.IDictionaryEnumerator enumerator = Myhashtable.getenumerator ();

while (enumerator. MoveNext ())
{
    Console.WriteLine (enumerator). Key);       Hashtable key word
    Console.WriteLine (enumerator. Value);      Hashtable value
}


Third, Dictionary Dictionary

Dictionary<tkey,tvalue> is a generic implementation of Hastbale.

<span style= "FONT-SIZE:18PX;" >code highlighting produced by Actipro Codehighlighter (freeware) http://www. codehighlighter.com/-->//traversal key
foreach (string key in Mydictionary.keys)
{
    //traverse the value of a key
    foreach ( String val in Mydictionary[key])
    {

    }
}</span>
Because Dictionary is a collection of keys and values, the element type is not a key type or a value type. In contrast, an element type is a key type and a value type KeyValuePair

<span style= "FONT-SIZE:18PX;" > Dictionary Traversal example 

Code highlighting produced by Actipro Codehighlighter (freeware) http://www. Codehighlighter.com/-->foreach (keyvaluepair<string, string> kvp in MyDictionary)
{
    string key = KVP . Key;//key contains the key for
    (int i = 0; i < KVP) in the dictionary. Value.count; i++)
    {
        Response.Write (KVP). Value[i]);
    }
</span>


Sample :

Code 

highlighting produced by Actipro Codehighlighter (freeware) http://www. codehighlighter.com/-->//defines a <string,int> Dictionary, lets its value be added (or Add method)
dictionary<string, int > dic = new dictionary<string, int> ();

Add two keys to "score 1", "Score 2" and give them a value of 0
dic["score 1"] = 0;
dic["Score 2" = 0;

Add the two values respectively to 1
dic["score 1"]++;
dic["Score 2"]++;





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.