HashTable、HashSet和Dictionary的區別

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

  2.HashSetspa

  HashSet<T>類主要是設計用來作高性能集運算的,例如對兩個集合求交集、並集、差集等。集合中包含一組不重複出現且無特性順序的元素,HashSet拒絕接受重複的對象pwa

  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 語句進行保護, 效率大減。

  (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);
        }
複製代碼
相關文章
相關標籤/搜索