C ා [difference between hashtable, HashSet and Dictionary]

[difference between HashTable, HashSet and Dictionary]

1.HashTable hash table

A Hashtable represents a collection of key / value pairs. In the * *. NET Framework * *, Hashtable is a container provided by the System.Collections namespace. It is used to process and represent key value pairs similar to key value, where key is usually used for quick search, and key is case sensitive; value is used to store the value corresponding to key. In Hashtable, key value key value pairs are of object type, so Hashtable can support any type of key,value 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,); 

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:

 HashtableObject.Contains(key);

2.HashSet hash set

The HashSet < T > class is mainly designed for high-performance set operations, such as intersection, union and difference of two sets. The collection contains a set of elements that do not recur and have no attribute order, and the HashSet rejects 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 added automatically as required.

3.Dictionary

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

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

Its characteristic is that the storage object needs to store 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 supports generics.

(2) the elements of hashtable belong to the Object type, so the operations of packing and unboxing usually occur when storing or retrieving value types, so you may need to perform some type conversion operations, and for int and float, these value types also need to be boxed, which is very time-consuming.

(3) it is recommended to use dictionary in single thread program, which has the advantage of generics, fast read speed and full use of capacity. It is recommended to use Hashtable in multithreaded programs. The default Hashtable allows single thread writing and multithreading reading. Further calling Synchronized() method on Hashtable can obtain the type of full thread safety. Dictionary is not thread safe, so it 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 the key is an integer type Dictionary is faster than that of the Hashtable. If the key is a string type, the efficiency of the Dictionary is not as fast as that of the Hashtable.

Demo Code

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

Tags: Programming Attribute

Posted on Wed, 11 Mar 2020 04:17:36 -0700 by shenmue232