Differences between HashTable, HashSet and Dictionary

I went to the interview again today, but the result is still very sad. I usually pay too much attention to the superficial things, but some essence is not clear. When I encounter the knowledge related to HashTable and Dictionary, I write it down. I hope it will be helpful to later people and can be used as a reference for my review in the future.

1.HashTable

A Hashtable represents a collection of key / value pairs. Yes NET Framework, Hashtable is system A container provided by the collections namespace, which is used to process and represent key value pairs similar to key value. Key can usually be used for quick search, and key is case sensitive; Value is used to store the value corresponding to the key. Key value pairs in Hashtable are of object type, so Hashtable can support any type of key value pairs, and any non null object can be used as a key or value.

Add a key / value pair to the hash table: hashtableobject Add(key,);  

Remove a hash value from a hashobject.hashkey Remove(key);

Remove all elements from hash table: hashtableobject Clear();

Judge whether the hash table contains a specific key key: hashtableobject Contains(key);

2.HashSet

HashSet class is mainly designed to do high-performance set operations, such as finding intersection, union and difference sets of two sets. The collection contains a set of elements that do not appear repeatedly and have no attribute order. HashSet rejects duplicate objects.

Some features of HashSet are as follows:

A. the values in HashSet cannot be repeated and have no order.

B. the capacity of HashSet will be automatically added as needed.

3.Dictionary

A Dictionary represents a collection of keys and values.

Dictionary < string, string > is a generic type

It has the function of collection. Sometimes it can be regarded as an array

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

Its characteristic is that the stored object needs to be stored in the generic type corresponding to the [key] value one by one

Find the corresponding value through a certain [key]

4. Differences between hashtable and Dictionary:

  (1).HashTable does not support generics, while Dictionary does.

  (2). The element of hashtable belongs to Object type, so boxing and unpacking operations usually occur when storing or retrieving value types, so you may need to carry out some type conversion operations, and boxing and other operations are also required for int and float value types, which is very time-consuming.

  (3). It is recommended to use Dictionary in single threaded programs, which has the advantage of generics, fast reading speed and full capacity utilization. It is recommended to use Hashtable in multithreaded programs. The default Hashtable allows single thread writing and multithreaded reading. Further calling the Synchronized() method on the Hashtable can obtain a fully thread safe type While Dictionary is not thread safe, it must be protected manually with lock statement, which greatly reduces the efficiency.

(4) when passing the code test, it is found that if the key is integer, the efficiency of Dictionary is faster than that of Hashtable. If the key is string, the efficiency of Dictionary is not as fast as that of Hashtable.

static void IntMethod()
        {
            int count = 1000000;
            Dictionary<int, int> dictionary = new Dictionary<int, int>();
            Hashtable hashtable = new Hashtable();
            for (int i = 0; i < count; i++)
            {
                dictionary.Add(i,i);
                hashtable.Add(i,i);
            }

            Stopwatch stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < count; i++)
            {
                int value = dictionary[i];
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < count; i++)
            {
                object value = hashtable[i];
            }
            stopwatch.Stop();

            Console.WriteLine(stopwatch.ElapsedMilliseconds);
 
        }

        static void MethodString()
        {
            int count = 1000000;
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            Hashtable hashtable=new Hashtable();
            for (int i = 0; i < count; i++)
            {
                dictionary.Add(i.ToString(),"aaa");
                hashtable.Add(i.ToString(),"aaa");
            }

            Stopwatch stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < count; i++)
            {
                string value=dictionary[i.ToString()];
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < count; i++)
            {
                object value = hashtable[i.ToString()];
            }
            stopwatch.Stop();

            Console.WriteLine(stopwatch.ElapsedMilliseconds);
        }

Transferred from: https://www.cnblogs.com/akwwl/p/3680376.html

Tags: C#

Posted by MFKR on Thu, 19 May 2022 07:48:44 +0300