HashSet hastable Dictionary concurrentdictionary Difference

Source: Internet
Author: User

1.HashTable

A hash table (HashTable) represents a collection of key/value pairs. In the. NET Framework, Hashtable is a container provided by the System.Collections namespace that handles and behaves like Key-value key-value pairs, where key is usually used for quick lookups, and key is case-sensitive; value is used to store the value corresponding to key. Key-value Key-value pairs in Hashtable are all object types, so hashtable can support any type of KeyValue key-value pair, and any non-null object can be used as a key or a value.

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 ();

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

  2.HashSet

The Hashset<t> class is designed to perform high-performance set operations such as intersection, set, and difference set for two sets. The collection contains a set of elements that do not recur and have no attribute order, and HashSet refuses to accept duplicate objects.

Some of the features of hashset<t> are as follows:

A. The values in hashset<t> cannot be duplicated and have no order.

B. The capacity of the hashset<t> is automatically added as needed.

  3.Dictionary

Dictionary represents a collection of keys and values.

Dictionary<string, string> is a generic

He has a set of functions that can sometimes be seen as an array.

His structure is this: Dictionary<[key], [value]>

His feature is that the deposit object is required to be stored with the [key] value one by one in the generic

To find the corresponding value by a certain [key]

  The difference between 4.HashTable and dictionary:

(1). Hashtable does not support generics, while dictionary supports generics.

(2). The Hashtable element is of type Object, so boxing and unpacking is usually the case when storing or retrieving a value type, so you may need to do some type conversion, and it can be time-consuming to int,float these value types for boxing.

(3). Dictionary is recommended in single-threaded programs, with a generic advantage, with faster read speeds and fuller capacity utilization. Hashtable is recommended in multithreaded programs, the default Hashtable allows single-threaded writes, multithreaded reads, and further calls to the Hashtable Synchronized () method to obtain a fully thread-safe type. While Dictionary is non-thread-safe, it must be protected by the use of the lock statement, and the efficiency is greatly reduced.

Concurrentdictionary Concurrentqueue concurrentstack Series is a thread-safe generic collection of. NET4.0, which is a thread-safe improved version of queue, Stack, and dictionary.

usingSystem;usingSystem.Diagnostics;usingSystem.Text;usingSystem.Collections;usingSystem.Collections.Generic;classtest{//both Hashtable and dictionary use hashing algorithms//The latter is generic and does not have a common packing-unboxing operation for the former//The latter is non-thread-safe, formerly thread-safe, and concurrentdirctionary is thread-safe//The latter parameter is a generic type and is flexible and type safe: The former is of type object, type is unsafe//    Static voidMain (string[] args) {Hashtable HT=NewHashtable (); Ht. ADD (1,1); ht[1] =1;//foreach (Keyvaluepair<object, object> p in HT)//This is not accessible because Hashtable is not generic and throws an exception//         {//Console.WriteLine (P.key + "," + p.value);//         }dictionary<int,int> dict =Newdictionary<int,int>(); Dict. ADD (1,1); dict[2] =2;//Normal        intx = dict[3] =3;//Normal//int y = dict[4];//Exception        foreach(keyvaluepair<int,int> Pinchdict) {Console.WriteLine (P.key+","+p.value); }        intXT; Dict. TryGetValue (5, outXT);//This is more efficient than judging containskey (5) and then dict[5], eliminating many of the traversalConsole.WriteLine (XT); }}

HashSet hastable Dictionary concurrentdictionary Difference

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.