using System.Collections; using System.Collections.Generic;
//添加一个keyvalue键值对: HashtableObject.Add(key,value); //移除某个keyvalue键值对: HashtableObject.Remove(key); //移除所有元素: HashtableObject.Clear(); // 判断是否包含特定键key: HashtableObject.Contains(key);
using System;
using System.Collections; //file使用Hashtable时,必须引入这个命名空间
class Program
{
public static void Main()
{
Hashtable ht = new Hashtable(); //创建一个Hashtable实例
ht.Add("北京", "帝都"); //添加keyvalue键值对
ht.Add("上海", "魔都");
ht.Add("广州", "省会");
ht.Add("深圳", "特区");
string capital = (string)ht["北京"];
Console.WriteLine(ht.Contains("上海")); //判断哈希表是否包含特定键,其返回值为true或false
ht.Remove("深圳"); //移除一个keyvalue键值对
ht.Clear(); //移除所有元素
}
}
using System;
using System.Collections;
class Program
{
static Hashtable GetHashtable()
{
Hashtable hashtable = new Hashtable();
hashtable.Add("名字", "小丽");
hashtable.Add("年龄", 22);
return hashtable;
}
static void Main()
{
Hashtable hashtable = GetHashtable();
string name = (string)hashtable["名字"];
Console.WriteLine(name);
int age = (int)hashtable["年龄"];
Console.WriteLine(age);
}
}
using System;
using System.Collections;
using System.IO;
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable.Add(100, "西安");
// 能转换成功
string value = hashtable[100] as string;
if (value != null)
{
Console.WriteLine(value);
}
// 转换失败,获取的值为null,但不会抛出错误。
StreamReader reader = hashtable[100] as StreamReader;
if (reader == null)
{
Console.WriteLine("西安不是StreamReader型");
}
// 也可以直接获取object值,再做判断
object value2 = hashtable[100];
if (value2 is string)
{
Console.Write("这个是字符串型: ");
Console.WriteLine(value2);
}
}
}
for(DictionaryEntry de in ht) //ht为一个Hashtable实例
{
Console.WriteLine(de.Key); //de.Key对应于keyvalue键值对key
Console.WriteLine(de.Value); //de.Key对应于keyvalue键值对value
}
foreach (int key in hashtable.Keys)
{
Console.WriteLine(key);
}
foreach (string value in hashtable.Values)
{
Console.WriteLine(value);
}
ArrayList akeys=new ArrayList(ht.Keys);
akeys.Sort(); //按字母顺序进行排序
foreach(string key in akeys)
{
Console.WriteLine(key + ": " + ht[key]); //排序后输出
}
Stopwatch sw = new Stopwatch();
Hashtable hashtable = new Hashtable();
Dictionary<string, int> dictionary = new Dictionary<string, int>();
int countNum = 1000000;
sw.Start();
for (int i = 0; i < countNum; i++)
{
hashtable.Add(i.ToString(), i);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds); //输出: 744
sw.Restart();
for (int i = 0; i < countNum; i++)
{
dictionary.Add(i.ToString(), i);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds); //输出: 489
sw.Restart();
for (int i = 0; i < countNum; i++)
{
hashtable.ContainsKey(i.ToString());
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds); //输出: 245
sw.Restart();
for (int i = 0; i < countNum; i++)
{
dictionary.ContainsKey(i.ToString());
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds); //输出: 192
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有