学习C# 哈希表(HashTable)用法

àì夳堔傛蜴生んèń 2021-07-26 19:38 513阅读 0赞

学习C# 哈希表(HashTable)用法

1. 哈希表(HashTable)简述

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

2. 哈希表使用情景

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

3. 哈希表的使用方法

哈希表需要使用的namespace:

  1. using System.Collections;
  2. using System.Collections.Generic;

哈希表的基本操作:

  1. //添加一个keyvalue键值对:
  2. HashtableObject.Add(key,value);
  3. //移除某个keyvalue键值对:
  4. HashtableObject.Remove(key);
  5. //移除所有元素:
  6. HashtableObject.Clear();
  7. // 判断是否包含特定键key:
  8. HashtableObject.Contains(key);

示例代码:

  1. using System;
  2. using System.Collections; //file使用Hashtable时,必须引入这个命名空间
  3. class Program
  4. {
  5. public static void Main()
  6. {
  7. Hashtable ht = new Hashtable(); //创建一个Hashtable实例
  8. ht.Add("北京", "帝都"); //添加keyvalue键值对
  9. ht.Add("上海", "魔都");
  10. ht.Add("广州", "省会");
  11. ht.Add("深圳", "特区");
  12. string capital = (string)ht["北京"];
  13. Console.WriteLine(ht.Contains("上海")); //判断哈希表是否包含特定键,其返回值为true或false
  14. ht.Remove("深圳"); //移除一个keyvalue键值对
  15. ht.Clear(); //移除所有元素
  16. }
  17. }

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

  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. class Program
  5. {
  6. static void Main()
  7. {
  8. Hashtable hashtable = new Hashtable();
  9. hashtable.Add(100, "西安");
  10. // 能转换成功
  11. string value = hashtable[100] as string;
  12. if (value != null)
  13. {
  14. Console.WriteLine(value);
  15. }
  16. // 转换失败,获取的值为null,但不会抛出错误。
  17. StreamReader reader = hashtable[100] as StreamReader;
  18. if (reader == null)
  19. {
  20. Console.WriteLine("西安不是StreamReader型");
  21. }
  22. // 也可以直接获取object值,再做判断
  23. object value2 = hashtable[100];
  24. if (value2 is string)
  25. {
  26. Console.Write("这个是字符串型: ");
  27. Console.WriteLine(value2);
  28. }
  29. }
  30. }

4. 遍历哈希表

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

  1. for(DictionaryEntry de in ht) //ht为一个Hashtable实例
  2. {
  3. Console.WriteLine(de.Key); //de.Key对应于keyvalue键值对key
  4. Console.WriteLine(de.Value); //de.Key对应于keyvalue键值对value
  5. }

遍历键值:

  1. foreach (int key in hashtable.Keys)
  2. {
  3. Console.WriteLine(key);
  4. }

遍历值:

  1. foreach (string value in hashtable.Values)
  2. {
  3. Console.WriteLine(value);
  4. }

5. 对哈希表进行排序

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

6. 哈希表的效率

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

  1. Stopwatch sw = new Stopwatch();
  2. Hashtable hashtable = new Hashtable();
  3. Dictionary<string, int> dictionary = new Dictionary<string, int>();
  4. int countNum = 1000000;
  5. sw.Start();
  6. for (int i = 0; i < countNum; i++)
  7. {
  8. hashtable.Add(i.ToString(), i);
  9. }
  10. sw.Stop();
  11. Console.WriteLine(sw.ElapsedMilliseconds); //输出: 744
  12. sw.Restart();
  13. for (int i = 0; i < countNum; i++)
  14. {
  15. dictionary.Add(i.ToString(), i);
  16. }
  17. sw.Stop();
  18. Console.WriteLine(sw.ElapsedMilliseconds); //输出: 489
  19. sw.Restart();
  20. for (int i = 0; i < countNum; i++)
  21. {
  22. hashtable.ContainsKey(i.ToString());
  23. }
  24. sw.Stop();
  25. Console.WriteLine(sw.ElapsedMilliseconds); //输出: 245
  26. sw.Restart();
  27. for (int i = 0; i < countNum; i++)
  28. {
  29. dictionary.ContainsKey(i.ToString());
  30. }
  31. sw.Stop();
  32. Console.WriteLine(sw.ElapsedMilliseconds); //输出: 192

由此可见,添加数据时Hashtable快。频繁调用数据时Dictionary快。
结论:Dictionary是泛型的,当K或V是值类型时,其速度远远超过Hashtable。

7. HashTable、HashSet和Dictionary的区别

(1). HashTable

哈希表(HashTable)表示键/值对的集合。在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key-value的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中key-value键值对均为object类型,所以Hashtable可以支持任何类型的keyvalue键值对,任何非 null 对象都可以用作键或值。
在哈希表中添加一个key/键值对:HashtableObject.Add(key,);

(2). HashSet

HashSet类主要是设计用来做高性能集运算的,例如对两个集合求交集、并集、差集等。集合中包含一组不重复出现且无特性顺序的元素,HashSet拒绝接受重复的对象。
HashSet的一些特性如下:

  • a. HashSet中的值不能重复且没有顺序。
  • b. HashSet的容量会按需自动添加。

(3). Dictionary

Dictionary表示键和值的集合。Dictionary是一个泛型,他本身有集合的功能有时候可以把它看成数组,他的结构是这样的:Dictionary<[key], [value]>,的特点是存入对象是需要与[key]值一一对应的存入该泛型,通过某一个一定的[key]去找到对应的值.

(4).HashTable和Dictionary的区别:

- a. HashTable不支持泛型,而Dictionary支持泛型。
  - b. Hashtable 的元素属于 Object 类型,所以在存储或检索值类型时通常发生装箱和拆箱的操作,所以你可能需要进行一些类型转换的操作,而且对于int,float这些值类型还需要进行装箱等操作,非常耗时。
  - c. 单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分。多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized() 方法可以获得完全线程安全的类型. 而 Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减。
  - d. 在通过代码测试的时候发现key是整数型Dictionary的效率比Hashtable快,如果key是字符串型,Dictionary的效率没有Hashtable快。

  1. static void IntMethod()
  2. {
  3. int count = 1000000;
  4. Dictionary<int, int> dictionary = new Dictionary<int, int>();
  5. Hashtable hashtable = new Hashtable();
  6. for (int i = 0; i < count; i++)
  7. {
  8. dictionary.Add(i,i);
  9. hashtable.Add(i,i);
  10. }
  11. Stopwatch stopwatch = Stopwatch.StartNew();
  12. for (int i = 0; i < count; i++)
  13. {
  14. int value = dictionary[i];
  15. }
  16. stopwatch.Stop();
  17. Console.WriteLine(stopwatch.ElapsedMilliseconds);
  18. stopwatch = Stopwatch.StartNew();
  19. for (int i = 0; i < count; i++)
  20. {
  21. object value = hashtable[i];
  22. }
  23. stopwatch.Stop();
  24. Console.WriteLine(stopwatch.ElapsedMilliseconds);
  25. }
  26. static void MethodString()
  27. {
  28. int count = 1000000;
  29. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  30. Hashtable hashtable=new Hashtable();
  31. for (int i = 0; i < count; i++)
  32. {
  33. dictionary.Add(i.ToString(),"aaa");
  34. hashtable.Add(i.ToString(),"aaa");
  35. }
  36. Stopwatch stopwatch = Stopwatch.StartNew();
  37. for (int i = 0; i < count; i++)
  38. {
  39. string value=dictionary[i.ToString()];
  40. }
  41. stopwatch.Stop();
  42. Console.WriteLine(stopwatch.ElapsedMilliseconds);
  43. stopwatch = Stopwatch.StartNew();
  44. for (int i = 0; i < count; i++)
  45. {
  46. object value = hashtable[i.ToString()];
  47. }
  48. stopwatch.Stop();
  49. Console.WriteLine(stopwatch.ElapsedMilliseconds);
  50. }

发表评论

表情:
评论列表 (有 0 条评论,513人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C# Hashtable

    一 哈希表的定义: 它使用键来访问集合中的元素。当您使用键访问元素时,则使用哈希表,而且您可以识别一个有用的键值。哈希表中的每一项都有一个键/值对。键用于访问集合中的项目。

    相关 HashTable /散列

    前言 在前面,我们提到过了几种搜索结构的树,二叉搜索树是根据左孩子节点的值小于根节点,右孩子节点的值大于根节点而建立的,当我们把n个数据插入到二叉搜索树中,最好情况下(数

    相关 HashTable /散列

    前言 在前面,我们提到过了几种搜索结构的树,二叉搜索树是根据左孩子节点的值小于根节点,右孩子节点的值大于根节点而建立的,当我们把n个数据插入到二叉搜索树中,最好情况下(数

    相关 c#】C#中的HashTable)详解

        哈希表是一种数据结构,它可以提供快速的插入操作和查找操作。第一次接触哈希表时,它的优点很多,无论是从哈希表中有多少数据,插入和删除只需要接近常量的时间即0(1)的时间级