• Hashtable


    Hashtable
    一,哈希表(Hashtable)简述

      在.NET work中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/的键值对,其中key通常可用来快速查找,同时key是区分大小写;用于存储对应于key的值。Hashtable中key/键值对均为object类型,所以Hashtable可以支持任何类型的key/键值对.

    二,哈希表的简单操作

     在哈希表中添加一个key/键值对:HashtableObject.Add(key,);
     在哈希表中去除某个key/键值对:HashtableObject.Remove(key);
     从哈希表中移除所有元素:           HashtableObject.Clear();
     判断哈希表是否包含特定键key:      HashtableObject.Contains(key);

     下面控制台程序将包含以上所有操作:
    using System;
    using System.Collections; //使用Hashtable时,必须引入这个命名空间

    class hashtable
    {
      public static void Main()
      {
      Hashtable ht=new Hashtable(); //创建一个Hashtable实例
      ht.Add("E","e");//添加key/键值对
      ht.Add("A","a");
      ht.Add("C","c");
      ht.Add("B","b");

      string s=(string)ht["A"];  //直接根据键获取值
      if(ht.Contains("E"))       //判断哈希表是否包含特定键,其返回值为true或false
        Console.WriteLine("the E key:exist");
      ht.Remove("C");//移除一个key/键值对
      Console.WriteLine(ht["A"]);//此处输出a
      ht.Clear();//移除所有元素
      Console.WriteLine(ht["A"]); //此处将不会有任何输出
      }
    }


    三,遍历哈希表

     遍历哈希表需要用到DictionaryEntry Object,代码如下:
     for(DictionaryEntry de in ht) //ht为一个Hashtable实例
     {
       Console.WriteLine(de.Key); //de.Key对应于key/键值对key
       Console.WriteLine(de.);    //de.Key对应于key/键值对
     }

    四,对哈希表进行排序

      对哈希表进行排序在这里的定义是对key/键值对中的key按一定规则重新排列,但是实际上这个定义是不能实现的,因为我们无法直接在Hashtable进行对key进行重新排列,如果需要Hashtable提供某种规则的输出,可以采用一种变通的做法:

     ArrayList akeys=new ArrayList(ht.Keys); //别忘了导入System.Collections
     // 这样可以提高查询速度
     akeys.Sort(); //按字母顺序进行排序
     for(string skey in akeys)
     {
       Console.Write(skey ":");
       Console.WriteLine(ht[skey]);//排序后输出
     }

    五 在哈希表存取类等其它对象
    // 类对象
    public class Person
    {
        private string FirstName;
        private string LastName;

        public Person(string first, string last)
        {
            FirstName = first;
            LastName = last;
        }

        public string FullName
        {
            get
            {
                return FirstName + " " + LastName;
            }
        }
    }

    // 存取过程
    protected void Page_Load(object sender, EventArgs e)
        {
            Person scott = new Person("Scott", "Hanselman");
            Person bill = new Person("Bill", "Evjen");
            Person srini = new Person("Srinivasa", "Sivakumar");

            Hashtable peopleHashtable = new Hashtable();
            peopleHashtable.Add("sh", scott);
            peopleHashtable.Add("be", bill);
            peopleHashtable.Add("ss", srini);

            Person found = (Person)peopleHashtable["sh"];
            Response.Write(found.FullName + "<BR/>");
            found = (Person)peopleHashtable["be"];
            Response.Write(found.FullName + "<BR/>");
            found = (Person)peopleHashtable["sh"];
            Response.Write(found.FullName + "<BR/>");

            ////write method 1:
            //foreach (DictionaryEntry de in peopleHashtable)
            //{
            //    Response.Write(de.Key.ToString() + ":" + ((Person)de.Value).FullName +
            //       "<BR/>");
            //}

            ////write method 2:
            //foreach (string s in peopleHashtable.Keys)
            //{
            //    Response.Write(s + "<BR/>");
            //}
            //foreach (Person p in peopleHashtable.Values)
            //{
            //    Response.Write(p.FullName + "<BR/>");
            //}
        }
    六 在hashtable中如何根据value读出key

    Hashtable t = new Hashtable();

    foreach( DictionaryEntry i in t )
    {
    if(  i.Value == "value1" ) return i.Key ;

    个人感觉

    Dictionary好像也是可以的,看的资料说是。hashtable是比dcitionary更先进点的,对我这样的菜鸟来说,功能好像都差不多的哦。
    Program that uses foreach on Dictionary: C#
    
    using System;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main()
        {
    	// Example Dictionary again
    	Dictionary<string, int> d = new Dictionary<string, int>()
    	{
    	    {"cat", 2},
    	    {"dog", 1},
    	    {"llama", 0},
    	    {"iguana", -1}
    	};
    	// Loop over pairs with foreach
    	foreach (KeyValuePair<string, int> pair in d)
    	{
    	    Console.WriteLine("{0}, {1}",
    		pair.Key,
    		pair.Value);
    	}
    	// Use var keyword to enumerate dictionary
    	foreach (var pair in d)
    	{
    	    Console.WriteLine("{0}, {1}",
    		pair.Key,
    		pair.Value);
    	}
        }
    }
    
    Output
    
    cat, 2
    dog, 1
    llama, 0
    iguana, -1
    
    cat, 2
    dog, 1
    llama, 0
    iguana, -1

    原文:http://blog.csdn.net/hunterxray/article/details/1546415

  • 相关阅读:
    Cuckoo for Hashing_双哈希表
    nyoj113_字符串替换
    nyoj366_D的小L_字典序_全排列
    二叉树的前序 中序 后序 遍历(递归/非递归)
    Java 学习路线
    leetcode 04 Median of Two Sorted Arrays
    ThreadLocal 的机制与内存泄漏
    try finally 执行顺序问题
    Java中的类加载器
    快速理解Java中的七种单例模式
  • 原文地址:https://www.cnblogs.com/http-www/p/3405064.html
Copyright © 2020-2023  润新知