C#中HashTable、Dictionary、ConcurrentDictionar三者都表示键/值对的集合,但是到底有什么区别,下面详细介绍

一、HashTable

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 类

二、Dictionary

Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射。通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictionary<TKey, TValue> 类是作为一个哈希表来实现的。检索速度取决于为 TKey 指定的类型的哈希算法的质量。TValue可以是值类型,数组,类或其他。

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

表示可由多个线程同时访问的键/值对的线程安全集合。

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());获得线程安全的对象)

转载于:https://www.cnblogs.com/mrray/p/10735815.html

C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别相关推荐

  1. HashTable,Dictionary,ConcurrentDictionary 的应用场景,区别,用法统计

    https://www.cnblogs.com/yinrq/p/5584885.html 一.HashTable HashTable表示键/值对的集合.在.NET Framework中,Hashtab ...

  2. C#中的集合、哈希表、泛型集合、字典

    集合ArrayList 集合:集合可以看做是数组,可以将其看成"长度可变,具有很多方法的数组".使用ArrayList,首先需要导入命名空间using.system.collect ...

  3. CSharp中的集合与字典:不同数据量的内存占用情况

    1. CSharp中的集合 集合(Collection)类是专门用于数据存储和检索的类.这些类提供了对栈(stack).队列(queue).列表(list)和哈希表(hash table)的支持.大多 ...

  4. C# 集合类型总结,ArrayList,Hastable,List<> 泛型,Dictionary<A, B> 字典集合

    C# 集合类型总结 文章目录 C# 集合类型总结 前言 1. ArrayList 列表集合 1.1 ArrayList集合方法(增删查改) 1.2 遍历ArrayList列表 2. Hastable ...

  5. python中字典和集合的区别_Python中字典和集合学习小结

    映射类型: 表示一个任意对象的集合,且可以通过另一个几乎是任意键值的集合进行索引 与序列不同,映射是无序的,通过键进行索引 任何不可变对象都可用作字典的键,如字符串.数字.元组等 包含可变对象的列表. ...

  6. 浅析几种数据集合(Dictionary、ArrayList、Hashtable和Array)的区别

    数组 Array: 数组是固定大小的,不能伸缩,要声明元素的类型. 数组可读可写不能声明只读数组:数组要有整数下标才能访问特定的元素 (强类型约束) ArrayList: 数组的复杂版本,动态数组类型 ...

  7. python基础一 -------如何在列表字典集合中根据条件筛选数据

    如何在列表字典集合中根据条件筛选数据 一:列表 先随机生成一个列表,过滤掉负数 1,普通for循环迭代判断 2,filter()函数判断,filter(函数,list|tuple|string) 1 ...

  8. python中列表 元组 字典 集合的区别

    参考文章:python中列表 元组 字典 集合的区别

  9. python字典中append_零基础入手!Python中字典与集合的使用指南

    全文共5092字,预计学习时长10分钟 本文将介绍Python的内置集合模块,用于支持集合和键值对等数学概念. 什么是集合? 集合是一组用于储存唯一值的序列. 初始化 可使用花括号{}定义集合. &g ...

最新文章

  1. 一打在2019年亮相的迷人科技项目:飞行汽车、子弹头列车、登月、……
  2. Java Mail 邮件发送(一):入门Demo
  3. 单选框-复选框重置的方法
  4. 解决 WIndows,Linux 以及 MacOS 终端无法使用代理的问题
  5. matlab句柄函数@和C++ 中的引用 很像
  6. 工作269:uni--客流分析优化
  7. [极客大挑战 2019]BabySQL
  8. 数字化转型方法论_老板让我搞数字化转型?成功之后,我整理了这套超全的方法论...
  9. 利用awstats分析nginx日志 简单配置
  10. 翻译 - 【Dojo Tutorials】Getting Started with dojox/app
  11. service network restart重启失败_NodeManager 启动失败问题处理
  12. 统计学习方法 第八章总结
  13. 随手记_C++常考面试题
  14. 计算机usb端口没反应,usb接口没反应,小编教你电脑usb接口没反应怎么解决
  15. 软考——论文写作基本介绍
  16. 计算机f2锁定用户,台式电脑键盘被锁住按什么键恢复 点锁定直接锁定计算机
  17. 谷歌浏览器插件 - 为 Microsoft Bing 搜索引擎首页添加一个【保存背景图片】的按钮(版本 2)
  18. 我为什么既支持又反对接口用Map来传输数据?
  19. java计算机毕业设计机械生产企业办公设备管理系统MyBatis+系统+LW文档+源码+调试部署
  20. Java泛型 T extends Serializable

热门文章

  1. HDU 3549 Flow Problem
  2. 解决SWFUpload在Chrome、Firefox浏览器下session找不到的问题
  3. 用vmware-converter4把linux 迁移到ESX4.1中
  4. Apache Lucene与Lucene.Net——全文检索服务器
  5. Scrapy运行中常见网络相关错误
  6. 非正常关闭vi编辑器时会生成一个.swp文件
  7. 输出某个目录下的所有文件和文件夹,包括子文件夹中的内容
  8. Lesson 03:运算符与流程控制
  9. UTF-8, Unicode, GB2312格式串转换之C语言版
  10. 数据结构——双向链表的实现