C#中哈希表(HashTable)的用法详解以及和Dictionary比较

1.  哈希表(HashTable)简述

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

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

(1)某些数据会被高频率查询
(2)数据量大
(3)查询字段包含字符串类型
(4)数据类型不唯一

 3. 哈希表的使用方法

哈希表需要使用的namespace

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);
    }
}
复制代码
复制代码

 

 当获取哈希表中数据时,如果类型声明的不对,会出现InvalidCastException错误。使用as-statements可以避免该错误。

复制代码
复制代码
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);
    }
    }
}
复制代码
复制代码

 

4. 遍历哈希表

 遍历哈希表需要用到DictionaryEntry Object,代码如下:

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

 

5. 对哈希表进行排序

  对哈希表按key值重新排列的做法:

ArrayList akeys=new ArrayList(ht.Keys); 
akeys.Sort(); //按字母顺序进行排序
foreach(string key in akeys)
{
   Console.WriteLine(key + ": " + ht[key]);  //排序后输出
}

 

6. 哈希表的效率

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

复制代码
复制代码
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
复制代码
复制代码

由此可见,添加数据时Hashtable快。频繁调用数据时Dictionary快。

结论:Dictionary<K,V>是泛型的,当K或V是值类型时,其速度远远超过Hashtable。

 

由于 Hashtable 和 Dictionary 同时存在, 在使用场景上必然存在选择性, 并不任何时刻都能相互替代.
[1] 单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分.
[2] 多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized() 方法可以获得完全线程安全的类型. 而 Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减.
[3] Dictionary 有按插入顺序排列数据的特性 (注: 但当调用 Remove() 删除过节点后顺序被打乱), 因此在需要体现顺序的情境中使用 Dictionary 能获得一定方便.

几种C#框架提供的数据结构对单值查找的效率比较->http://www.cnblogs.com/eaglet/archive/2008/10/23/1317893.html
我个人是觉得,无论什么时候,都应该使用Dictionary<K,V>,理由如下:
1、Dic是类型安全的,这有助于我们写出更健壮更具可读性的代码,而且省却我们强制转化的麻烦。这个相信大家都明白。
2、Dic是泛行的,当K或V是值类型时,其速度远远超过Hashtable。这个大家对值类型与引用类型有所了解的话也会明白。

原因:Dictionary 类与 Hashtable 类的功能相同。对于值类型,特定类型(不包括 Object)的 Dictionary 的性能优于 Hashtable,这是因为 Hashtable 的元素属于 Object 类型,所以在存储或检索值类型时通常发生装箱和取消装箱操作。
3、如果K和V都是引用类型,如eaglet所测,Hashtable比Dic更快,这里我要指出,eaglet所做的测试是有问题的。原因在于Hashtable与Dic采用的是不同的数据结构。eaglet的“Dictionary 由于在Hashtable基础上封装了一层”这个说法是不对的。

Dictionary 调用 Add 方法之前使用 ContainsKey 方法测试某个键是否存在,否则得到一个KeyNotFoundException。
当程序频繁尝试字典中不存在的键时,使用 TryGetValue 方法来检索值,这种方法是一种更有效的检索值的方法。

http://www.cnblogs.com/lucifer1982/archive/2008/06/18/1224319.html 
http://www.cnblogs.com/lucifer1982/archive/2008/07/03/1234431.html

Hashtable在指定capacity参数时,它并不只开出capacity个槽的内存空间,而是开出比 capacity / 0.72(默认装填因子) 大的最小素数个槽的空间;而Dic在指定capacity时,是开出 比capacity 大的最小素数个槽的空间。因此可以看到,楼主虽然都指定capacity为10万,而实际上Hashtable的槽的总数远远大于Dic的槽的总数,也就是占用的内存远远大于Dic,因此,如此测试是不公平不公正的,如要公平公正的测试,则应该把Dic的capacity指定为 10万/0.72,请大家再测试其性能。


url:http://www.cnblogs.com/jhh0111/archive/2008/10/23/1318223.html

我认为应该始终使用Dictionary<K, V>,即使要用Hashtable了,也可以用Dictionary<object, object>来替代。

猜你喜欢

转载自blog.csdn.net/gaojinjingg/article/details/80980703