3月6日 Hashtable和Dictionary

Profile Picture
- Published on Mar 6, 2020🌏 Public

Hashtable和Dictionary

Hashtable

HashTable中文名为散列列表,他的特点是: 1. 根据键(Key)去读取和存储数据。 2. 键值不能相同,键的类型可以是任意类型 3. 可以存储任意类型数据,数据取出时是object类型

Hashtable table = new Hashtable();

//添加
table.Add("a", "abcd");
table.Add(1, 1234);
// table.Add(1, 1234);  //报错,Key不可以重复
var cat = new Cat();
table.Add(cat,"cat");
           
//判断某个键是否穿在
if(table.ContainsKey(cat))
{
    string a = (string) table[cat];
    Console.WriteLine("存在,值为" + table[cat]);
}
//修改
table[cat] = 1;
table.Remove(cat);

//遍历
foreach (var key in table.Keys)
{
    Console.WriteLine(key+":" + table[key]);
}

Dictionary

字典与Hashtable的区别在于,字典有限制键和值的数据类型。

//Dictionary<键类型,值类型> 
Dictionary<string,string> table = new Dictionary<string, string>() { 
    { "1","22"} ,
    { "2","33"}
};

//添加
table.Add("a", "abcd");
           
//判断某个键是否穿在
if(table.ContainsKey("1"))
{
    string a = (string) table["1"];
    Console.WriteLine("存在,值为" + table["1"]);
}
//修改
table["1"] = "111";
table.Remove("1");

//遍历
foreach (var key in table.Keys)
{
    Console.WriteLine(key+":" + table[key]);
}

验证字典对于查找数据的速度到底有没有帮助

创建人员信息:身份证号,姓名

使用for循环创建100000000条人员信息,分别存入列表与字典。

为列表和字典实现查询的方法。

输入要查询的内容,选择使用列表查询还是字典查询。看要多久

    class Program
    {
        static void Main(string[] args)
        {
            List<Cat> catList = new List<Cat>();
            Dictionary<string, Cat> catDic = new Dictionary<string, Cat>();
            for (int i = 0; i < 10000000; i++)
            {
                var cat = new Cat();
                cat.Id = i.ToString("000000000000000000");
                cat.Name = "猫" + i;
                catList.Add(cat);
                catDic.Add(cat.Id, cat);
            }
            Console.WriteLine("任意键开始");
            Console.ReadKey();
            Test(catList, catDic);
        
        }
       
        static void Test(List<Cat> catList, Dictionary<string, Cat> catDic)
        {
            var r = new Random();

            List<string> idList = new List<string>();

            #region 创建要查找的id列表
            for (int i = 0; i < 100; i++)
            {
                var n = r.Next(0, 10000000);
                idList.Add(n.ToString("000000000000000000"));
            }
            #endregion

            #region 列表查询
            DateTime t1 = DateTime.Now;
            for (int i = 0; i < idList.Count; i++)
            {
                for (int p = 0; p < catList.Count; p++)
                {
                    if (catList[p].Id == idList[i])
                    {
                        break; //找到后中断循环
                    }
                }
            }
            DateTime t2 = DateTime.Now;
            Console.WriteLine("List耗时:"+(t2 - t1).TotalMilliseconds);
            #endregion

            #region 字典查询
            DateTime t3 = DateTime.Now;
            for (int i = 0; i < idList.Count; i++)
            {
                var s = catDic[idList[i]]; //找到数据赋值到s变量
            }
            DateTime t4 = DateTime.Now;
            Console.WriteLine("Dic耗时:"+(t4 - t3).TotalMilliseconds);
            #endregion

            Console.ReadKey();
        }
    }

    class Cat
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }