1、HashTable定義
System.Collections. Hashtable類表示鍵/值對的集合,這些鍵/值對根據鍵的哈希代碼進行組織, 每個元素都是一個存儲在 DictionaryEntry 對象中的鍵/值對。鍵不能為 null,但值可以。
2.優點
1、通過Key快速查找。
2、Hashtable 是線程安全的。
3. Hashtable的構造器
構造器函數 |
注釋 |
Public Hashtable () |
使用默認的初始容量(容量大小為0)、加載因子、哈希代碼提供程序和比較器來初始化 Hashtable 類的新的空實例。 |
public Hashtable (IDictionary) |
通過將指定字典中的元素復制到新的 Hashtable 對象中,初始化 Hashtable 類的一個新實例。新 Hashtable 對象的初始容量等於復制的元素數,並且使用默認的加載因子、哈希代碼提供程序和比較器。 |
public Hashtable (Int32) |
使用指定的初始容量、默認加載因子、默認哈希代碼提供程序和默認比較器來初始化 Hashtable 類的新的空實例。 |
4、Hashtable的屬性
屬性名 |
注釋 |
獲取包含在 Hashtable 中的鍵/值對的數目。 |
|
獲取一個值,該值指示 Hashtable 是否具有固定大小。 |
|
獲取一個值,該值指示 Hashtable 是否為只讀。 |
|
獲取包含 Hashtable 中的鍵的 ICollection。 |
|
獲取包含 Hashtable 中的值的 ICollection。 |
5. Hashtable的方法
方法名 |
注釋 |
Void Add(object key,object value) |
將帶有指定鍵和值的元素添加到 Hashtable 中。 |
Void Clear() |
從 Hashtable 中移除所有元素。 |
Bool Contains(object key) |
確定 Hashtable 是否包含特定鍵。 |
Bool ContainsKey(object key) |
確定 Hashtable 是否包含特定鍵。 |
Bool ContainsValue(object value) |
確定 Hashtable 是否包含特定值。 |
Void Remove(object key) |
從 Hashtable 中移除帶有指定鍵的元素。 |
Void InsertRange(int index,Icollection collec) |
用於從指定位置開始添加一批元素,列表后面的元素依次往后移動 |
Clone() |
創建 Hashtable 的淺表副本。 |
實現 ISerializable 接口,並返回序列化 Hashtable 所需的數據。 |
6、Hashtable的使用示例

{
public static void Main( string [] args)
{
// 創建一個HashTable
Hashtable openWith = new Hashtable();
// 為HashTable添加元素,不能有重復的key,但可以有重復的值
openWith.Add( " txt " , " notepad.exe " );
openWith.Add( " bmp " , " paint.exe " );
openWith.Add( " dib " , " paint.exe " );
openWith.Add( " rtf " , " wordpad.exe " );
// 添加重復的key,會拋出異常
try
{
openWith.Add( " txt " , " winword.exe " );
}
catch
{
Console.WriteLine( " An element with Key = \ " txt\ " already exists. " );
}
// 通過key獲得值
Console.WriteLine( " For key = \ " rtf\ " , value = {0}. " , openWith[ " rtf " ]);
// 重新賦值
openWith[ " rtf " ] = " winword.exe " ;
Console.WriteLine( " For key = \ " rtf\ " , value = {0}. " , openWith[ " rtf " ]);
// 以賦值的方式,創建一個新元素
openWith[ " doc " ] = " winword.exe " ;
// 如果HashTable中不包含該元素,將拋出異常(經測試這里不拋出異常)
// 原因(如果未找到指定的鍵,嘗試獲取它將返回 空引用(在 Visual Basic 中為 Nothing),嘗試設置它將使用指定的鍵創建新元素。 )
try
{
Console.WriteLine( " For key = \ " tif\ " , value = {0}. " , openWith[ " tif " ]);
}
catch
{
Console.WriteLine( " Key = \ " tif\ " is not found. " );
}
// 判斷是否包含特定的key
if ( ! openWith.ContainsKey( " ht " ))
{
openWith.Add( " ht " , " hypertrm.exe " );
Console.WriteLine( " Value added for key = \ " ht\ " : {0} " , openWith[ " ht " ]);
}
// 遍歷HashTable
Console.WriteLine();
foreach (DictionaryEntry de in openWith)
{
Console.WriteLine( " Key = {0}, Value = {1} " , de.Key, de.Value);
}
// 獲取HashTable中值的集合
ICollection valueColl = openWith.Values;
Console.WriteLine();
foreach ( string s in valueColl)
{
Console.WriteLine( " Value = {0} " , s);
}
// 獲取HashTable中鍵的集合
ICollection keyColl = openWith.Keys;
Console.WriteLine();
foreach ( string s in keyColl)
{
Console.WriteLine( " Key = {0} " , s);
}
Console.WriteLine( " \nRemove(\ " doc\ " ) " );
// 移除指定的元素
openWith.Remove( " doc " );
if ( ! openWith.ContainsKey( " doc " ))
{
Console.WriteLine( " Key \ " doc\ " is not found. " );
}
Hashtable mySourceHT = new Hashtable();
mySourceHT.Add( " A " , " valueA " );
mySourceHT.Add( " B " , " valueB " );
// 創建一個字符串數組
String[] myTargetArray = new String[ 15 ];
myTargetArray[ 0 ] = " The " ;
myTargetArray[ 1 ] = " quick " ;
myTargetArray[ 2 ] = " brown " ;
myTargetArray[ 3 ] = " fox " ;
myTargetArray[ 4 ] = " jumped " ;
myTargetArray[ 5 ] = " over " ;
myTargetArray[ 6 ] = " the " ;
myTargetArray[ 7 ] = " lazy " ;
myTargetArray[ 8 ] = " dog " ;
// 遍歷數組的值
Console.WriteLine( " The target Array contains the following before: " );
PrintValues(myTargetArray, ' ' );
// 將hashtable中的key復制到數組中
Console.WriteLine( " After copying the keys, starting at index 6: " );
mySourceHT.Keys.CopyTo(myTargetArray, 6 );
PrintValues(myTargetArray, ' ' );
// 將hashtable中的Value復制到數組中
Console.WriteLine( " After copying the values, starting at index 6: " );
mySourceHT.Values.CopyTo(myTargetArray, 6 );
PrintValues(myTargetArray, ' ' );
Console.Read();
}
// 遍歷數據方法
public static void PrintValues(String[] myArr, char mySeparator)
{
for ( int i = 0 ; i < myArr.Length; i ++ )
Console.Write( " {0}{1} " , mySeparator, myArr[i]);
Console.WriteLine();
}
}
7、Hashtable遍歷方法
方法一
foreach (System.Collections.DictionaryEntry objDE in objHasTab)
{
Console.WriteLine(objDE.Key.ToString());
Console.WriteLine(objDE.Value.ToString());
}
方法二
System.Collections.IDictionaryEnumerator enumerator = objHashTablet.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine(enumerator.Key); // Hashtable關健字
Console.WriteLine
}
8、Hashtable排序
//把ht的鍵對象全部復制到ArrayList中
ArrayList al = new ArrayList(ht.Keys);
/*ht.Keys返回ht中所有鍵對象構成的集合,把該集合傳遞給ArrayList構造方法則得到一個包
*所有鍵對象的動態數組
*/
al.Sort();//從小到大排列
//排序完成輸出
for (int i = 0; i < al.Count;i++ )
{
object e=al[i];
object temp = (object)ht[e];//鍵作為索引器來獲得對應的值對象
Console.WriteLine(temp.tostring());
}