【學習筆記】Hashtable幾種常用的遍歷方法

Hashtable

在System.Collection是命名空間李Hashtable是程序員經常用到的類,它以快速檢索著稱,是研發人員開發當中不可缺少的利器。

Hashtable表示鍵/值對的集合,這些鍵/值對根據鍵的哈希代碼進行組織。Hashtable的鍵必須是唯一的,沒有有效的排序,他進行的是內在的排序。

Hashtable有以下4中遍歷方式

              1、以string對象爲鍵值遍歷哈希表。

              2、以自定義對象爲鍵值遍歷哈希表。

              3、以DictionaryEntry對象爲鍵值遍歷哈希表。

              4、通過繼承IDictionaryEnumerator接口的對象來遍歷哈希表。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        class Person
        {
            private int age;
            public int Age
            {
                get { return age; }
                set { age = value; }
            }
            private string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            private string email;
            public string Email
            {
                get { return email; }
                set { email = value; }
            }
        }
        static void Main(string[] args)
        {
            var a = new Person
            {
                Age = 34,
                Name = "Jacky",
                Email = "[email protected]"
            };

            var b = new Person
            {
                Age = 23,
                Name = "Ajay",
                Email = "[email protected]"
            };

            var c = new Person
            {
                Age = 12,
                Name = "Bill",
                Email = "[email protected]"
            };

            var d = new Person
            {
                Age = 23,
                Name = "Gace",
                Email = "[email protected]"
            };

            var e = new Person
            {
                Age = 45,
                Name = "Jim",
                Email = "[email protected]"
            };
            var ht = new Hashtable
            {
                { "1", a },
                { "2", b },
                { "3", c },
                { "4", d },
                { "5", e }
            };

            Console.WriteLine("請輸入你的查詢的用戶名:");
            var strName = Console.ReadLine();

            //第一種方法
            foreach (string item in ht.Keys)
            {
                var p = (Person)ht[item];
                if (strName == p.Name)
                {
                    Console.WriteLine("查詢後的結果是:" + p.Name + "\t" + p.Email + "\t" + p.Age);
                }
            }
            Console.WriteLine("華麗的分割線=========================================================");
            //第二種方法
            foreach (Person item in ht.Values)
            {
                if (item.Name == strName)
                {
                    Console.WriteLine("查詢後的結果是:" + item.Name + "\t" + item.Email + "\t" + item.Age);
                }
            }
            Console.WriteLine("華麗的分割線=========================================================");
            //第三種方法
            foreach (DictionaryEntry item in ht)
            {
                if (strName == ((Person)item.Value).Name)
                {
                    Console.WriteLine("查詢後的結果是:" + ((Person)item.Value).Name + "\t" + ((Person)item.Value).Email + "\t" + ((Person)item.Value).Age);
                }
            }
            Console.WriteLine("華麗的分割線=========================================================");
            //第四種方法
            IDictionaryEnumerator id = ht.GetEnumerator();
            while (id.MoveNext())
            {
                Person p = (Person)ht[id.Key];
                if (p.Name == strName)
                {
                    Console.WriteLine("查詢後的結果是:" + p.Name + "\t" + p.Email + "\t" + p.Age);
                }
            }
            Console.ReadKey();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章