HashTable中的key/value均为object类型,由包含集合元素的存储桶组成。存储桶是 HashTable中各元素的虚拟子组,与大多数集合中进行的搜索和检索相比,存储桶可令搜索和检索更为便捷。每一存储桶都与一个哈希代码关联,该哈希代 码是使用哈希函数生成的并基于该元素的键。HashTable的优点就在于其索引的方式,速度非常快。如果以任意类型键值访问其中元素会快于其他集合,特 别是当数据量特别大的时候,效率差别尤其大。

HashTable的应用场合有:做对象缓存,树递归算法的替代,和各种需提升效率的场合。

//Hashtable sample
     System.Collections.Hashtable ht = new System.Collections.Hashtable();

//--Be careful: Keys can't be duplicated, and can't be null----
     ht.Add(1, "apple");
     ht.Add(2, "banana");
     ht.Add(3, "orange");
    
     //Modify item value:
     if(ht.ContainsKey(1))
         ht[1] = "appleBad";

//The following code will return null oValue, no exception
     object oValue = ht[5];  
    
     //traversal 1:
     foreach (DictionaryEntry de in ht)
     {
         Console.WriteLine(de.Key);
         Console.WriteLine(de.Value);
     }

//traversal 2:
     System.Collections.IDictionaryEnumerator d = ht.GetEnumerator();
     while (d.MoveNext())
     {
         Console.WriteLine("key:{0} value:{1}", d.Entry.Key, d.Entry.Value);
     }

//Clear items
     ht.Clear();

Dictionary和HashTable内部实现差不多,但前者无需装箱拆箱操作,效率略高一点。

//Dictionary sample
     System.Collections.Generic.Dictionary<int, string> fruits =
         new System.Collections.Generic.Dictionary<int, string>();

fruits.Add(1, "apple");
     fruits.Add(2, "banana");
     fruits.Add(3, "orange");

foreach (int i in fruits.Keys)
     {
         Console.WriteLine("key:{0} value:{1}", i, fruits);
     }

if (fruits.ContainsKey(1))
     {
         Console.WriteLine("contain this key.");
     }

ArrayList是一维变长数组,内部值为object类型,效率一般:

//ArrayList
     System.Collections.ArrayList list = new System.Collections.ArrayList();
     list.Add(1);//object type
     list.Add(2);
     for (int i = 0; i < list.Count; i++)
     {
         Console.WriteLine(list);
     }

HashTable是经过优化的,访问下标的对象先散列过,所以内部是无序散列的,保证了高效率,也就是说,其输出不是按照开始加入的顺序,而 Dictionary遍历输出的顺序,就是加入的顺序,这点与Hashtable不同。如果一定要排序HashTable输出,只能自己实现:

//Hashtable sorting
     System.Collections.ArrayList akeys = new System.Collections.ArrayList(ht.Keys); //from Hashtable
     akeys.Sort(); //Sort by leading letter
     foreach (string skey in akeys)
     {
         Console.Write(skey + ":");
         Console.WriteLine(ht[skey]);
     }

HashTable与线程安全:

为了保证在多线程的情况下的线程同步访问安全,微软提供了自动线程同步的HashTable:

如果 HashTable要允许并发读但只能一个线程写, 要这么创建 HashTable实例:

//Thread safe HashTable
     System.Collections.Hashtable htSyn = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
这样, 如果有多个线程并发的企图写HashTable里面的 item, 则同一时刻只能有一个线程写, 其余阻塞; 对读的线程则不受影响。

另外一种方法就是使用lock语句,但要lock的不是HashTable,而是其SyncRoot;虽然不推荐这种方法,但效果一样的,因为源代码就是这样实现的:

//Thread safe
     private static Hashtable htCache = new Hashtable();

public static void AccessCache()
     {
         lock (htCache.SyncRoot)
         {
             //Do something
         }
     }

转载于:https://www.cnblogs.com/silverLee/archive/2009/11/05/1596774.html

C#集合类(HashTable, Dictionary, ArrayList,List)与HashTable线程安全相关推荐

  1. C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全

    HashTable中的key/value均为object类型,由包含集合元素的存储桶组成.存储桶是 HashTable中各元素的虚拟子组,与大多数集合中进行的搜索和检索相比,存储桶可令搜索和检索更为便 ...

  2. C#方法,可空类型,数组,集合,ArrayList排序,List,Hashtable和Dictionary

    C#方法 方法的定义: public void/int Compare(int a,int b){ } Program program = new Program(); Console.WriteLi ...

  3. Vector ArrayList Hashtable HashMap ArrayList LinkedList

    1. Vector & ArrayList 1)  Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的方法不是,由于线程的 ...

  4. Hashtable Dictionary的使用

    要了解C#中Hashtable Dictionary的使用,我们先来看一个例子! using System; using System.Collections; namespace NoSortHas ...

  5. Lession11 集合和泛型(ArrayList方法、Arraylist类、ArrayList添加对象、ArrayList长度、HashTable类、Hashtable类练习-----)

    目录 ArrayList方法: ArrayList添加对象: Arraylist类: ArrayList长度: HashTable类: Hashtable类练习: IComparable泛型接口排序: ...

  6. HashMap、HashTable、ConcurrentHashMap、HashSet区别 线程安全类

    HashMap专题:HashMap的实现原理--链表散列 HashTable专题:Hashtable数据存储结构-遍历规则,Hash类型的复杂度为啥都是O(1)-源码分析 Hash,Tree数据结构时 ...

  7. .Net/C# 实现真正的只读的 Hashtable 类型的属性 (ReadOnly Hashtable Property)

    /* .Net/C# 实现真正的只读属性 (ReadOnly Property) 当类的私有成员是简单类型时,只需为该成员提供 public { get; } 的访问器即可实现只读属性. 当类的私有成 ...

  8. Java Colections 集合类 —— List、ArrayList、Set(HashSet)

    capacity 对于 ArrayList,以 1.5 的速率增长,对于 HashMap 则以 2 倍的速率增长. 继承关系如下: 0. List<T> 是一个接口 该接口定义的高级成员函 ...

  9. java hashtable 并发_Java 并发容器 —— Hashtable 与 Collections.synchronizedMap(HashMap) 的区别...

    Hashtable 部分源码 以 Hashtable 的 put 方法为例: Hashtable 保证线程安全的方式在 方法前加上 synchronized 关键字(锁的是类的实例) Collecti ...

最新文章

  1. MyCat - 使用篇(1)
  2. 滴滴重磅开源跨平台统一 MVVM 框架 Chameleon
  3. 怎么在eclipse里调试WebDriver的源代码
  4. python -c带来的惊喜
  5. html语言制作网页,HTML语言的网页制作技巧与方法
  6. 云栖大会 | 开源引力峰会线下参会指南
  7. centos 7上ambari安装试用
  8. 吐血整理,2021年最新【阿里、头条、美团】【软件测试】面试题(持续更新!)
  9. C语言:求矩阵对角线元素的和
  10. 实现原理 扫描枪_条码扫描枪的工作原理
  11. 中文字体库转换成16X16点阵 另外附上ASCII码转换8X16点阵
  12. UART 通信 协议 (二)
  13. 哲理故事300篇 上
  14. freeswitch1.10.7 安装部署排坑
  15. 朴素贝叶斯算法,点进来了解了解。
  16. 北京信息工程学院考研计算机,2017届信息工程学院考研光荣榜
  17. 分布式系统(Distributed Systems)概述
  18. 导航电子地图制作资质的办理指南
  19. 帝国php忘记密码,一键帝国CMS快速重置管理员密码工具(送给忘记密码的站长)...
  20. 福昕PDF编辑器把PDF转换为富文本格式Word

热门文章

  1. PHP-开发环境搭建
  2. windows计划任务启动bat执行java文件
  3. 【bzoj1179】 Apio2009—Atm
  4. Oracle_11g_R2安装手册(图文教程)
  5. 用matlab绘制抛物线y的x平方,我刚刚学习MATLAB,想画一下(x^2+y^2-1)^3=x^2*y^3这个曲线的图像,该怎么画呢,谢谢大家了。...
  6. 对角矩阵和类下三角矩阵的频率和质量数据比较
  7. python中axis是什么意思_Python axis的含义
  8. 【UGV】32版UGV原理图
  9. 【Matlab】求解积分方程的数值解
  10. 【数理知识】《随机过程》方兆本老师-第6章-鞅过程及其性质