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[i]);
}
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 System.Collections.Hashtable htCache = new System.Collections.Hashtable ();
public static void AccessCache ()
{
lock
( htCache.SyncRoot )
{
htCache.Add ( "key" , "value" );
//Be careful: don't use foreach to operation on the whole collection
//Otherwise the collection won't be locked correctly even though indicated locked
//--by MSDN
}
}
//Is equivalent to 等同于 (lock is equivalent to Monitor.Enter and Exit()
public static void AccessCache ()
{
System.Threading.Monitor.Enter ( htCache.SyncRoot );
try
{
/* critical section */
htCache.Add ( "key" , "value" );
//Be careful: don't use foreach to operation on the whole collection
//Otherwise the collection won't be locked correctly even though indicated locked
//--by MSDN
}
finally
{
System.Threading.Monitor.Exit ( htCache.SyncRoot );
}
}
本文详细介绍了HashTable的数据结构及其在多线程环境下的应用。包括其内部实现原理、如何进行元素的添加和检索,以及如何保证线程安全。同时对比了HashTable与Dictionary、ArrayList的不同之处。
339





