C#中HashTable、Dictionary芙粱、ConcurrentDictionar三者都表示鍵/值對(duì)的集合祭玉,但是到底有什么區(qū)別,下面詳細(xì)介紹
一春畔、HashTable
HashTable表示鍵/值對(duì)的集合脱货。在.NET Framework中,Hashtable是System.Collections命名空間提供的一個(gè)容器律姨,用于處理和表現(xiàn)類(lèi)似key-value的鍵值對(duì)振峻,其中key通常可用來(lái)快速查找择份,同時(shí)key是區(qū)分大小寫(xiě)扣孟;value用于存儲(chǔ)對(duì)應(yīng)于key的值。Hashtable中key-value鍵值對(duì)均為object類(lèi)型荣赶,所以Hashtable可以支持任何類(lèi)型的keyvalue鍵值對(duì)凤价,任何非 null 對(duì)象都可以用作鍵或值。
HashTable是一種散列表讯壶,他內(nèi)部維護(hù)很多對(duì)Key-Value鍵值對(duì)料仗,其還有一個(gè)類(lèi)似索引的值叫做散列值(HashCode),它是根據(jù)GetHashCode方法對(duì)Key通過(guò)一定算法獲取得到的伏蚊,所有的查找操作定位操作都是基于散列值來(lái)實(shí)現(xiàn)找到對(duì)應(yīng)的Key和Value值的立轧。
散列函數(shù)(GetHashCode)讓散列值對(duì)應(yīng)HashTable的空間地址盡量不重復(fù)。
當(dāng)一個(gè)HashTable被占用一大半的時(shí)候我們通過(guò)計(jì)算散列值取得的地址值可能會(huì)重復(fù)指向同一地址躏吊,這就造成哈希沖突氛改。
C#中鍵值對(duì)在HashTable中的位置Position= (HashCode& 0x7FFFFFFF) % HashTable.Length,C#是通過(guò)探測(cè)法解決哈希沖突的比伏,當(dāng)通過(guò)散列值取得的位置Postion以及被占用的時(shí)候胜卤,就會(huì)增加一個(gè)位移x值判斷下一個(gè)位置Postion+x是否被占用,如果仍然被占用就繼續(xù)往下位移x判斷Position+2*x位置是否被占用赁项,如果沒(méi)有被占用則將值放入其中葛躏。當(dāng)HashTable中的可用空間越來(lái)越小時(shí)快骗,則獲取得到可用空間的難度越來(lái)越大郭毕,消耗的時(shí)間就越多。
使用方法如下:
<pre style="margin-top:0px;margin-bottom:0px;padding:0px;white-space:pre-wrap;font-family:'Courier New' !important;">using System; using System.Collections; namespace WebApp
{ class Program
{ static void Main(string[] args)
{
Hashtable myHash=new Hashtable(); //插入
myHash.Add("1","joye.net");
myHash.Add("2", "joye.net2");
myHash.Add("3", "joye.net3"); //key 存在
try {
myHash.Add("1", "1joye.net");
} catch {
Console.WriteLine("Key = "1" already exists.");
} //取值
Console.WriteLine("key = "2", value = {0}.", myHash["2"]); //修改
myHash["2"] = "http://www.cnblogs.com/yinrq/";
myHash["4"] = "joye.net4"; //修改的key不存在則新增
Console.WriteLine("key = "2", value = {0}.", myHash["2"]);
Console.WriteLine("key = "4", value = {0}.", myHash["4"]); //判斷key是否存在
if (!myHash.ContainsKey("5"))
{
myHash.Add("5", "joye.net5");
Console.WriteLine("key = "5": {0}", myHash["5"]);
} //移除
myHash.Remove("1"); if (!myHash.ContainsKey("1"))
{
Console.WriteLine("Key "1" is not found.");
} //foreach 取值
foreach (DictionaryEntry item in myHash)
{
Console.WriteLine("Key = {0}, Value = {1}", item.Key, item.Value);
} //所有的值
foreach (var item in myHash.Values)
{
Console.WriteLine("Value = {0}",item);
} //所有的key
foreach (var item in myHash.Keys)
{
Console.WriteLine("Key = {0}", item);
}
Console.ReadKey();
}
}
}</pre>
結(jié)果如下:
更多參考微軟官方文檔:Hashtable 類(lèi)
二、Dictionary
Dictionary<TKey, TValue> 泛型類(lèi)提供了從一組鍵到一組值的映射汁掠。通過(guò)鍵來(lái)檢索值的速度是非陈壅快的卤恳,接近于 O(1)萄喳,這是因?yàn)?Dictionary<TKey, TValue> 類(lèi)是作為一個(gè)哈希表來(lái)實(shí)現(xiàn)的。檢索速度取決于為 TKey 指定的類(lèi)型的哈希算法的質(zhì)量猾愿。TValue可以是值類(lèi)型鹦聪,數(shù)組,類(lèi)或其他蒂秘。
Dictionary是一種變種的HashTable,它采用一種分離鏈接散列表的數(shù)據(jù)結(jié)構(gòu)來(lái)解決哈希沖突的問(wèn)題泽本。
簡(jiǎn)單使用代碼:
<pre style="margin-top:0px;margin-bottom:0px;padding:0px;white-space:pre-wrap;font-family:'Courier New' !important;">using System; using System.Collections; using System.Collections.Generic; namespace WebApp
{ class Program
{ static void Main(string[] args)
{
Dictionary<string, string> myDic = new Dictionary<string, string>(); //插入
myDic.Add("1", "joye.net");
myDic.Add("2", "joye.net2");
myDic.Add("3", "joye.net3"); //key 存在
try {
myDic.Add("1", "1joye.net");
} catch {
Console.WriteLine("Key = "1" already exists.");
} //取值
Console.WriteLine("key = "2", value = {0}.", myDic["2"]); //修改
myDic["2"] = "http://www.cnblogs.com/yinrq/";
myDic["4"] = "joye.net4"; //修改的key不存在則新增
Console.WriteLine("key = "2", value = {0}.", myDic["2"]);
Console.WriteLine("key = "4", value = {0}.", myDic["4"]); //判斷key是否存在
if (!myDic.ContainsKey("5"))
{
myDic.Add("5", "joye.net5");
Console.WriteLine("key = "5": {0}", myDic["5"]);
} //移除
myDic.Remove("1"); if (!myDic.ContainsKey("1"))
{
Console.WriteLine("Key "1" is not found.");
} //foreach 取值
foreach (var item in myDic)
{
Console.WriteLine("Key = {0}, Value = {1}", item.Key, item.Value);
} //所有的值
foreach (var item in myDic.Values)
{
Console.WriteLine("Value = {0}",item);
} //所有的key
foreach (var item in myDic.Keys)
{
Console.WriteLine("Key = {0}", item);
}
Console.ReadKey();
}
}
}</pre>
運(yùn)行結(jié)果:
更多資料參考:Dictionary 類(lèi)
三、ConcurrentDictionary
表示可由多個(gè)線程同時(shí)訪問(wèn)的鍵/值對(duì)的線程安全集合材彪。
ConcurrentDictionary<TKey, TValue> framework4出現(xiàn)的观挎,可由多個(gè)線程同時(shí)訪問(wèn),且線程安全段化。用法同Dictionary很多相同嘁捷,但是多了一些方法。ConcurrentDictionary 屬于System.Collections.Concurrent 命名空間按照MSDN上所說(shuō):
System.Collections.Concurrent 命名空間提供多個(gè)線程安全集合類(lèi)显熏。當(dāng)有多個(gè)線程并發(fā)訪問(wèn)集合時(shí)雄嚣,應(yīng)使用這些類(lèi)代替 System.Collections 和 System.Collections.Generic 命名空間中的對(duì)應(yīng)類(lèi)型。
更多資料:ConcurrentDictionary<TKey,?TValue> 類(lèi)
四喘蟆、對(duì)比總結(jié)
分別插入500萬(wàn)條數(shù)據(jù)缓升,然后遍歷,看看耗時(shí)蕴轨。
<pre style="margin-top:0px;margin-bottom:0px;padding:0px;white-space:pre-wrap;font-family:'Courier New' !important;">using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; namespace WebApp
{ class Program
{ static Hashtable _hashtable; static Dictionary<string, string> _dictionary; static ConcurrentDictionary<string, string> _conDictionary; static void Main(string[] args)
{
Compare(5000000);
Console.ReadLine();
Console.Read();
} public static void Compare(int dataCount)
{
_hashtable = new Hashtable();
_dictionary = new Dictionary<string, string>();
_conDictionary=new ConcurrentDictionary<string, string>();
Stopwatch stopWatch = new Stopwatch(); // Hashtable
stopWatch.Start(); for (int i = 0; i < dataCount; i++)
{
_hashtable.Add("key" + i.ToString(), "Value" + i.ToString());
}
stopWatch.Stop();
Console.WriteLine("HashTable插" + dataCount + "條耗時(shí)(毫秒):" + stopWatch.ElapsedMilliseconds); //Dictionary
stopWatch.Reset();
stopWatch.Start(); for (int i = 0; i < dataCount; i++)
{
_dictionary.Add("key" + i.ToString(), "Value" +i.ToString());
}
stopWatch.Stop();
Console.WriteLine("Dictionary插" + dataCount + "條耗時(shí)(毫秒):" + stopWatch.ElapsedMilliseconds); //ConcurrentDictionary
stopWatch.Reset();
stopWatch.Start(); for (int i = 0; i < dataCount; i++)
{
_conDictionary.TryAdd("key" + i.ToString(), "Value" + i.ToString());
}
stopWatch.Stop();
Console.WriteLine("ConcurrentDictionary插" + dataCount + "條耗時(shí)(毫秒):" + stopWatch.ElapsedMilliseconds); // Hashtable
stopWatch.Reset();
stopWatch.Start(); for (int i = 0; i < _hashtable.Count; i++)
{ var key = _hashtable[i];
}
stopWatch.Stop();
Console.WriteLine("HashTable遍歷時(shí)間(毫秒):" + stopWatch.ElapsedMilliseconds); //Dictionary
stopWatch.Reset();
stopWatch.Start(); for (int i = 0; i < _hashtable.Count; i++)
{ var key = _dictionary["key" + i.ToString()];
}
stopWatch.Stop();
Console.WriteLine("Dictionary遍歷時(shí)間(毫秒):" + stopWatch.ElapsedMilliseconds); //ConcurrentDictionary
stopWatch.Reset();
stopWatch.Start(); for (int i = 0; i < _hashtable.Count; i++)
{ var key = _conDictionary["key"+i.ToString()];
}
stopWatch.Stop();
Console.WriteLine("ConcurrentDictionary遍歷時(shí)間(毫秒):" + stopWatch.ElapsedMilliseconds);
}
}
}</pre>
運(yùn)行結(jié)果:
可以看出:
大數(shù)據(jù)插入Dictionary花費(fèi)時(shí)間最少
遍歷HashTable最快是Dictionary的1/5,ConcurrentDictionary的1/10
單線程建議用Dictionary港谊,多線程建議用ConcurrentDictionary或者HashTable(Hashtable tab = Hashtable.Synchronized(new Hashtable());獲得線程安全的對(duì)象)