Hashtable
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
在.net framework中,hashtable是system.collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。hashtable中key/value键值对均为object类型,所以hashtable可以支持任何类型的key/value键值对.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace HashTableTest
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add("a","a");
ht.Add("b","b");
ht.Add("c","c");
ht.Add("e","e");
ht.Add("d","d");
Console.WriteLine("
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
htSort(ht);
Console.WriteLine("
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
foreach (DictionaryEntry de in ht)//实例
{
Console.WriteLine(de.Value);
}
Console.WriteLine("
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
string s = (string)ht["a"];
Console.WriteLine("(string)ht[\"a\"]={0}",s);
s = ht["a"].ToString();
Console.WriteLine("ht[\"a\"] = {0}",s);
Console.WriteLine("
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
if (ht.ContainsKey("e"))
{
Console.WriteLine("Contain the key of \"e\"");
}
ht.Remove("e");
Console.WriteLine("Remove \"e\"");
if (!ht.Contains("e"))
{
Console.WriteLine("It does not Contains the key of \"e\"");
}
Console.ReadKey();
ht.Clear();
}
static void htSort(Hashtable table)
{
ArrayList akeys = new ArrayList(table.Keys);
akeys.Sort();//按字母顺序排列
foreach (string skey in akeys)
{
Console.Write(skey);
Console.WriteLine("
![](https://www.cnblogs.com/Images/dot.gif)
}
}
}
}
///
遍历哈希表需要用到dictionaryentry object,代码如下:
foreach(dictionaryentry de in ht) //ht为一个hashtable实例
{
console.writeline(de.key);//de.key对应于key/value键值对key
console.writeline(de.value);//de.key对应于key/value键值对value
}
四,对哈希表进行排序
对哈希表进行排序在这里的定义是对key/value键值对中的key按一定规则重新排列,但是实际上这个定义是不能实现的,因为我们无法直接在hashtable进行对key进行重新排列,如果需要hashtable提供某种规则的输出,可以采用一种变通的做法:
arraylist akeys=new arraylist(ht.keys); //别忘了导入system.collections
akeys.sort(); //按字母顺序进行排序
foreach(string skey in akeys)
{
console.write(skey + ":");
console.writeline(ht[skey]);//排序后输出
}