小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C#中哈希表(HashTable)的用法詳解

 kiki的號(hào) 2017-04-25

1.  哈希表(HashTable)簡(jiǎn)述


  在.NET Framework中,Hashtable是System.Collections命名空間提供的一個(gè)容器,用于處理和表現(xiàn)類似keyvalue的鍵值對(duì),其中key通??捎脕砜焖俨檎遥瑫r(shí)key是區(qū)分大小寫;value用于存儲(chǔ)對(duì)應(yīng)于key的值。Hashtable中keyvalue鍵值對(duì)均為object類型,所以Hashtable可以支持任何類型的keyvalue鍵值對(duì).


2. 什么情況下使用哈希表


(1)某些數(shù)據(jù)會(huì)被高頻率查詢
(2)數(shù)據(jù)量大
(3)查詢字段包含字符串類型
(4)數(shù)據(jù)類型不唯一


 3. 哈希表的使用方法


哈希表需要使用的namespace



using System.Collections;
using System.Collections.Generic;


 


哈希表的基本操作:


復(fù)制代碼

//添加一個(gè)keyvalue鍵值對(duì):
HashtableObject.Add(key,value);

//移除某個(gè)keyvalue鍵值對(duì):
HashtableObject.Remove(key);

//移除所有元素:           
HashtableObject.Clear(); 

// 判斷是否包含特定鍵key:
HashtableObject.Contains(key);

復(fù)制代碼

 


控制臺(tái)程序例子:


復(fù)制代碼

using System;
using System.Collections; //file使用Hashtable時(shí),必須引入這個(gè)命名空間
class Program
{
  public static void Main()
  {
     Hashtable ht = new Hashtable(); //創(chuàng)建一個(gè)Hashtable實(shí)例
     ht.Add("北京", "帝都"); //添加keyvalue鍵值對(duì)
     ht.Add("上海", "魔都");
     ht.Add("廣州", "省會(huì)");
     ht.Add("深圳", "特區(qū)");

     string capital = (string)ht["北京"];
     Console.WriteLine(ht.Contains("上海")); //判斷哈希表是否包含特定鍵,其返回值為true或false
     ht.Remove("深圳"); //移除一個(gè)keyvalue鍵值對(duì)
     ht.Clear(); //移除所有元素
  }
}     

復(fù)制代碼

 


 哈希表中使用多種數(shù)據(jù)類型的例子:


復(fù)制代碼

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);
    }
}

復(fù)制代碼

 


 當(dāng)獲取哈希表中數(shù)據(jù)時(shí),如果類型聲明的不對(duì),會(huì)出現(xiàn)InvalidCastException錯(cuò)誤。使用as-statements可以避免該錯(cuò)誤。


復(fù)制代碼

using System;
using System.Collections;
using System.IO;

class Program
{
    static void Main()
    {
    Hashtable hashtable = new Hashtable();
    hashtable.Add(100, "西安");

    // 能轉(zhuǎn)換成功
    string value = hashtable[100] as string;
    if (value != null)
    {
        Console.WriteLine(value);
    }

    // 轉(zhuǎn)換失敗,獲取的值為null,但不會(huì)拋出錯(cuò)誤。
    StreamReader reader = hashtable[100] as StreamReader;

    if (reader == null)
    {
         Console.WriteLine("西安不是StreamReader型");
    }

    // 也可以直接獲取object值,再做判斷
    object value2 = hashtable[100];
    if (value2 is string)
    {
        Console.Write("這個(gè)是字符串型: ");
        Console.WriteLine(value2);
    }
    }
}

復(fù)制代碼

 


4. 遍歷哈希表


 遍歷哈希表需要用到DictionaryEntry Object,代碼如下:



for(DictionaryEntry de in ht) //ht為一個(gè)Hashtable實(shí)例
{
   Console.WriteLine(de.Key);  //de.Key對(duì)應(yīng)于keyvalue鍵值對(duì)key
   Console.WriteLine(de.Value);  //de.Key對(duì)應(yīng)于keyvalue鍵值對(duì)value
}


 


遍歷鍵



foreach (int key in hashtable.Keys)
{
    Console.WriteLine(key);
}


 


遍歷值



foreach (string value in hashtable.Values)
{
    Console.WriteLine(value);
}


 


5. 對(duì)哈希表進(jìn)行排序


  對(duì)哈希表按key值重新排列的做法:



ArrayList akeys=new ArrayList(ht.Keys); 
akeys.Sort(); //按字母順序進(jìn)行排序
foreach(string key in akeys)
{
   Console.WriteLine(key + ": " + ht[key]);  //排序后輸出
}


 


6. 哈希表的效率


System.Collections下的哈希表(Hashtable)和System.Collections.Generic下的字典(Dictionary)都可用作lookup table,下面比較一下二者的執(zhí)行效率。


復(fù)制代碼

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

復(fù)制代碼

由此可見,添加數(shù)據(jù)時(shí)Hashtable快。頻繁調(diào)用數(shù)據(jù)時(shí)Dictionary快。


結(jié)論:Dictionary<K,V>是泛型的,當(dāng)K或V是值類型時(shí),其速度遠(yuǎn)遠(yuǎn)超過Hashtable。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多