C # hashset, HashTable, dictionary The Difference "turn"

Source: Internet
Author: User

HashSet is similar to the set in Python, all prepared for logical operations, HashSet does not allow duplication of data, and the time single value is not a key-value pair.
Hashtable and dictionary similar, but their implementation of different, dictionary commonly known as the dictionary, which stored in the time key value pair, that is, KeyValuePair, and support generics, and Hashtable domestic generally translated as a hash table, but in my opinion, In order to better express its essence, translation for the hash table is better, because the hashtable inside the village key when the hash is stored in the way, but dictionary inside is in order to save KeyValuePair.

Practice to remember to live, so the Leetcode No. 500 is entitled to illustrate this point.

PublicString[]Findwords (String[] words) {Forint i=0;i<words. length;i++) {Words[i] = Words[i]. ToLower (); }String arr1 ="Qwertyuiop";String arr2 ="ASDFGHJKL";String ARR3 ="ZXCVBNM"; hashset<Int> Map1 =New hashset<Int> (); dictionary<CharInt> MAP2 =New dictionary<CharInt> (); Hashtable MAP3 =New Hashtable ();Forint i=0;i<arr1. length;i++) {Map1. ADD (i); Map2. ADD (Arr1[i],1); Map3. ADD (Arr1[i],1); }Forint i=0; I<arr2. length;i++) {Map1. ADD (i); Map2. ADD (Arr2[i],2); Map3. ADD (Arr2[i],2); }Forint i=0; I<arr3. length;i++) {Map1. ADD (i); Map2. ADD (Arr3[i],2); Map3. ADD (Arr3[i],2); } int flag = 0; List<string> lststr = new List< string> (); for (int i=0; i<words. length;i++) {flag = 0; for (int j=0;j<words[i]. length;j++) {if (Map2[words[i][0] [! = Map2[words[i][j]]) {flag = 1; break;} } if (flag = = 0) Lststr.add (Words[i]);} return Lststr.toarray (); }

Where Map1 is Hashset,map2 is dictionary, MAP3 is hashtable, but what if the map2 in the If judgment is changed to MAP3? Look at this section of code.

object a = 1; object b = 1; Console.WriteLine(a.GetType()); if(a==b) { Console.WriteLine("a==b"); }

The result is a not equal to B, for what, because they have the same content but the address is different, but the = = to equals the comparison can be, specifically please see the difference between equals and = = (note here slightly around OH).

The problem here is that Hashtable always treats all variables as objects, and the dictionary type is the type that specifies key and value.

In addition dictionary data structure again single-threaded use will be faster, the value of the type will be stored faster than Hashtable, this is because Hashtable for the number of deposit, whether the value type or reference type will become the object type about this, look at a small example.

79625374

C # hashset, HashTable, dictionary The Difference "turn"

Related Article

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.