1. 程式人生 > >HashTable、HashSet和Dictionary的區別(轉載)

HashTable、HashSet和Dictionary的區別(轉載)

The 調用 測試 opera lock-free api 結構 通過 屬於

1.HashTable
哈希表(HashTable)表示鍵/值對的集合。在.NET Framework中,Hashtable是System.Collections命名空間提供的一個容器,用於處理和表現類似key-value的鍵值對,其中key通常可用來快速查找,同時key是區分大小寫;value用於存儲對應於key的值。Hashtable中key-value鍵值對均為object類型,所以Hashtable可以支持任何類型的keyvalue鍵值對,任何非 null 對象都可以用作鍵或值。 在哈希表中添加一個key/鍵值對:HashtableObject.Add(key,);  在哈希表中去除某個key/鍵值對:HashtableObject.Remove(key); 從哈希表中移除所有元素: HashtableObject.Clear(); 判斷哈希表是否包含特定鍵key: HashtableObject.Contains(key);
  
2.HashSet HashSet<T>類主要是設計用來做高性能集運算的,例如對兩個集合求交集、並集、差集等。集合中包含一組不重復出現且無特性順序的元素,HashSet拒絕接受重復的對象。
  
HashSet<T>的一些特性如下:
  a. HashSet<T>中的值不能重復且沒有順序。
  b. HashSet<T>的容量會按需自動添加。
  
3.Dictionary
Dictionary表示鍵和值的集合。Dictionary<string, string>是一個泛型,他本身有集合的功能有時候可以把它看成數組
他的結構是這樣的:Dictionary<[key], [value]>
他的特點是存入對象是需要與[key]值一一對應的存入該泛型
通過某一個一定的[key]去找到對應的值
  
4.HashTable和Dictionary的區別:

1.HashTable不支持泛型,而Dictionary支持泛型。

2.Hashtable 的元素屬於 Object 類型,所以在存儲或檢索值類型時通常發生裝箱和拆箱的操作,所以你可能需要進行一些類型轉換的操作,而且對於int,float這些值類型還需要進行裝箱等操作,非常耗時。

3.單線程程序中推薦使用 Dictionary, 有泛型優勢, 且讀取速度較快, 容量利用更充分。多線程程序中推薦使用 Hashtable, 默認的 Hashtable 允許單線程寫入, 多線程讀取, 對 Hashtable 進一步調用 Synchronized() 方法可以獲得完全線程安全的類型

. 而 Dictionary 非線程安全, 必須人為使用 lock 語句進行保護, 效率大減。

關於HashTable的線程安全,微軟MSDN的解釋如下:

Hashtable is thread safe for use by multiple reader threads and a single writing thread. It is thread safe for multi-thread use when only one of the threads perform write (update) operations, which allows for lock-free reads provided that the writers are serialized to the Hashtable. To support multiple writers all operations on the Hashtable must be done through the wrapper returned by the Synchronized(Hashtable) method, provided that there are no threads reading the Hashtable object. 

4.在通過代碼測試的時候發現key是整數型Dictionary的效率比Hashtable快,如果key是字符串型,Dictionary的效率沒有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);
        }

微軟HashTable介紹鏈接

原文鏈接

HashTable、HashSet和Dictionary的區別(轉載)