Differences between hashtable and Dictionary

Source: Internet
Author: User

1: dictionary is recommended in a single-threaded program. It has the wildcard advantage, fast reading speed, and sufficient capacity utilization.
2: hashtable is recommended in multi-threaded programs. The default hashtable allows single-threaded writing and multi-threaded reading. For hashtable, the synchronized () method can be further called to obtain a completely thread-safe type. dictionary is NOT thread-safe and must be manually protected using the lock statement, greatly reducing the efficiency.
3: dictionary has the ability to sort data by insertion Order (Note: however, when the remove () function is called to delete a node, the order is disrupted ), therefore, it is convenient to use a dictionary to reflect the order.

 

Hashtable class and dictionary <(of <(tkey, tvalue>) generic class implement idictionary Interface

Dictionary <(of <(tkey, tvalue>) generic classes also implement idictionary <(of <(tkey, tvalue>) Generic interfaces. Therefore, each element in these sets is a key/value pair.

Dictionary <(of <(tkey, tvalue>) has the same functions as hashtable.
For value types, the performance of the Dictionary of a specific type (excluding objects) <(of <(tkey, tvalue>)> is better than that of hashtable, because the hashtable element belongs to the object type, therefore, the packing and unboxing operations are often triggered when the value type is stored or retrieved.

Hashtable ht = new hashtable (); // implement the idictionary Interface
Ht. Add (1, "");
Ht. Add (2, "B ");
Ht. Add (3, "C ");
Foreach (dictionaryentry de in HT) // hashtable returns the dictionaryentry type
{
De. Key;
De. value;
}

Dictionary <int, string> mydictionary = new dictionary <int, string> (); // implements the idictionary interface and the idictionary <t key, T value> class.
Mydictionary. Add (1, "");
Mydictionary. Add (2, "B ");
Mydictionary. Add (3, "C ");
Foreach (int I in mydictionary. Keys)
{
Console. writeline ("Key =" + I + "value =" + mydictionary );
}
Or
Foreach (keyvaluepair <string, double> temp in mydictionary) // The returned keyvaluepair <string, double> Generic Array
{
Temp. Key;
Temp. value;
}

// An exception occurs if the key does not exist when you use the indexer.
Try
{
Console. writeline ("nonexistent key" "fff" "has the following key values:" + mydic ["fff"]);
}
Catch (keynotfoundexception ex)
{
Console. writeline ("the key is not found and an exception is thrown:" + ex. Message );
}
// The method to solve the above exception is to use contarnskey () to determine if a key exists. If you need to calculate the key value frequently, it is best to use trygetvalue to obtain the corresponding key value in the set.
String value = "";
If (mydictionary. trygetvalue ("fff", out value ))
{
Console. writeline ("nonexistent key" "fff" "has the following key values:" + value );
}
Else
{
Console. writeline ("the corresponding key value is not found ");
}

// Use foreach below to Traverse Key-value pairs
// The generic struct is used to store the key-value pair.
Foreach (keyvaluepair <string, string> temp in mydictionary)
{
Console. writeline (temp. Key, temp. value );
}
// Obtain a set of values
Foreach (string s in mydictionary. values)
{
Console. writeline ("value" + S );
}
// Another method is worth obtaining
Dictionary <string, string>. valuecollection values = mydictionary. values;
Foreach (string s in values)
{
Console. writeline ("value:" + S );
}

 

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.