C#中HashTable、Dictionary、ConcurrentDictionar三者都表示键/值对的集合,可是到底有什么区别,下面详细介绍算法
HashTable表示键/值对的集合。在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现相似key-value的键值对,其中key一般可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中key-value键值对均为object类型,因此Hashtable能够支持任何类型的keyvalue键值对,任何非 null 对象均可以用做键或值。数组
HashTable是一种散列表,他内部维护不少对Key-Value键值对,其还有一个相似索引的值叫作散列值(HashCode),它是根据GetHashCode方法对Key经过必定算法获取获得的,全部的查找操做定位操做都是基于散列值来实现找到对应的Key和Value值的。安全
散列函数(GetHashCode)让散列值对应HashTable的空间地址尽可能不重复。数据结构
当一个HashTable被占用一大半的时候咱们经过计算散列值取得的地址值可能会重复指向同一地址,这就形成哈希冲突。多线程
C#中键值对在HashTable中的位置Position= (HashCode& 0x7FFFFFFF) % HashTable.Length,C#是经过探测法解决哈希冲突的,当经过散列值取得的位置Postion以及被占用的时候,就会增长一个位移x值判断下一个位置Postion+x是否被占用,若是仍然被占用就继续往下位移x判断Position+2*x位置是否被占用,若是没有被占用则将值放入其中。当HashTable中的可用空间愈来愈小时,则获取获得可用空间的难度愈来愈大,消耗的时间就越多。并发
使用方法以下:函数
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(); } } }
结果以下:大数据
更多参考微软官方文档:Hashtable 类spa
Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射。经过键来检索值的速度是很是快的,接近于 O(1),这是由于 Dictionary<TKey, TValue> 类是做为一个哈希表来实现的。检索速度取决于为 TKey 指定的类型的哈希算法的质量。TValue能够是值类型,数组,类或其余。.net
Dictionary是一种变种的HashTable,它采用一种分离连接散列表的数据结构来解决哈希冲突的问题。
简单使用代码:
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(); } } }
运行结果:
更多资料参考:Dictionary 类
表示可由多个线程同时访问的键/值对的线程安全集合。
ConcurrentDictionary<TKey, TValue> framework4出现的,可由多个线程同时访问,且线程安全。用法同Dictionary不少相同,可是多了一些方法。ConcurrentDictionary 属于System.Collections.Concurrent 命名空间按照MSDN上所说:
System.Collections.Concurrent 命名空间提供多个线程安全集合类。当有多个线程并发访问集合时,应使用这些类代替 System.Collections 和 System.Collections.Generic 命名空间中的对应类型。
更多资料:ConcurrentDictionary<TKey,?TValue> 类
分别插入500万条数据,而后遍历,看看耗时。
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 + "条耗时(毫秒):" + 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 + "条耗时(毫秒):" + 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 + "条耗时(毫秒):" + stopWatch.ElapsedMilliseconds); // Hashtable stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < _hashtable.Count; i++) { var key = _hashtable[i]; } stopWatch.Stop(); Console.WriteLine("HashTable遍历时间(毫秒):" + 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遍历时间(毫秒):" + 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遍历时间(毫秒):" + stopWatch.ElapsedMilliseconds); } } }
运行结果:
能够看出:
大数据插入Dictionary花费时间最少
遍历HashTable最快是Dictionary的1/5,ConcurrentDictionary的1/10
单线程建议用Dictionary,多线程建议用ConcurrentDictionary或者HashTable(Hashtable tab = Hashtable.Synchronized(new Hashtable());得到线程安全的对象)