A summary of the differences between hashtable, HashSet and dictionary

Time:2021-2-12

Today, I went to the interview again, but the result is still very sad. I usually pay too much attention to the superficial things, but some of the substance is not very clear. When I encounter the knowledge related to hashtable and dictionary, I want to write it down, hoping to be helpful for later generations and for my future review.

1.HashTable

A hash table represents a collection of key / value pairs. In. Net framework, hashtable is System.Collections Namespace provides a container for processing and representing key value pairs similar to key value, in which key can be used for quick search, and key is case sensitive; value is used to store the value corresponding to key. The key value pairs in hashtable are all 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 / key value pair to the hash table: HashtableObject.Add (key,);

To remove a key / key value pair from the hash table: HashtableObject.Remove (key);

Remove all elements from the hash table: HashtableObject.Clear ();

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

2.HashSet

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

Some features of HashSet < T > are as follows:

a. The values in HashSet < T > cannot be repeated and have no order.

b. The capacity of HashSet < T > will be automatically added on demand.

3.Dictionary

A dictionary represents a collection of keys and values.

Dictionary < string, string > is a generic

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

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

Its characteristic is that the stored object needs to be stored in the generic 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 supports generics.

(2) The element of. Hashtable belongs to the object type, so boxing and unboxing operations usually occur when storing or retrieving value types, so you may need to do some type conversion operations, and boxing operations are also needed for int and float value types, which is very time-consuming.

(3) Dictionary is recommended for single threaded programs, which has the advantages of generics, faster reading speed and more full utilization of capacity. Hashtable is recommended in multithreaded programs. The default hashtable allows single thread writing and multi thread reading. Further calling synchronized() method on hashtable can obtain a completely thread safe type. Dictionary is not thread safe and must be protected by using lock statement artificially, which greatly reduces the efficiency.

(4) When passing the code test, it is found that the efficiency of key is integer type, and dictionary is faster than hashtable. If the key is string type, the efficiency of dictionary is not as fast as 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);
    }

The biggest harvest of today’s interview is not to summarize the above things, but to let me understand that many things should not pursue the surface, not to complete any function, but to learn a certain technology, we should further understand its essence, and the foundation is very important. The three interviewers are all excellent. They talked about many things I seldom use. When I communicate with them, I will learn a lot. I really hope I can enter a company like this in the future and grow up quickly.

Interview, not only to find a satisfactory job, from which you can learn how to communicate, how to sell yourself, more importantly, let yourself understand their own shortcomings, in which aspects they are still lacking, still need to be improved.

This article about the differences between hashtable, HashSet and dictionary is introduced here. For more information about the differences between hashtable, HashSet and dictionary, please search the previous articles of developer or continue to browse the following articles. I hope you can support developer more in the future!