C#中使用Dictionary实现Map数据结构

在VC中使用过CMap以及在Java中使用过Map的朋友应该很熟悉,使用Map可以方便实现基于键值对数据的处理,在C#中,你就需要实现IDictionary接口的关键字值集合类,允许通过关键字(如字符串名)进行索引,这也可以使用索引符来完成,但这种索引符参数是与存储的项相关联的关键字,而不是int索引。通常情况下,我们使用泛型类Dictionary来实现这样的功能。

Dictionary泛型类提供快速的基于键值的元素查找,Dictionary<K, V>是一个泛型 ,他本身有集合的功能有时候可以把它看成数组,它的结构是这样的:Dictionary<[key], [value]> ,他的特点是存入对象是需要与[key]值一一对应的存入该泛型 ,通过某一个一定的[key]去找到对应的值。Dictionary<K, V>泛型类在定义变量是需要指定当前存储的键值对类型,如:

1

Dictionary< string , string > hashMap = new Dictionary< string , string >();

补充说明:
1、必须包含名空间System.Collection.Generic
2、Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)
3、键必须是唯一的,而值不需要唯一的
4、键和值都可以是任何类型(比如:string, int, 自定义类型,等等)
5、通过一个键读取一个值的时间是接近O(1)
6、键值对之间的偏序可以不定义

首先给出一个使用泛型类Dictionary<K, V>的完整例子,其中包含了创建和初始化一个Dictionary对象、判断是否存在相应的key并显示、遍历所有的Keys、遍历所有的Values、遍历整个字典。
C#代码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

/// <summary> 

/// Dictionary的基本用法 

/// </summary> 

static void
DictionaryDemo()

{

     Dictionary< int , string > dict = new Dictionary< int , string >();

     dict.Add(1, "111" );

     dict.Add(2, "222" );

     //判断是否存在相应的key并显示 

     if (dict.ContainsKey(2))

     {

         Console.WriteLine(dict[2]);

     }

     //遍历Keys 

     foreach (var item in dict.Keys)

     {

         Console.WriteLine( "Key:{0}" , item);

     }

     //遍历Values 

     foreach (var item in dict.Values)

     {

         Console.WriteLine( "value:{0}" , item);

     }

     //遍历整个字典 

     foreach (var item in dict)

     {

         Console.WriteLine( "key:{0} value:{1}" , item.Key, item.Value);

     }

}

下面对一些比较重要的用法进行补充介绍:

1.在字典Dictionary<K, V>中查找指定键的元素


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

static void
Test( string [] args)

{

     Dictionary< string , int > d = new Dictionary< string , int >();

     d.Add( "C#" , 2);

     d.Add( "VB" , 1);

     d.Add( "C" , 0);

     d.Add( "C++" , -1);

     //判断字典中是否包含键为“VB”的元素

     if (d.ContainsKey( "VB" )) // True

     {

         int p = d[ "VB" ];

         Console.WriteLine(p);

     }

     //判断字典中是否包含键为“C”的元素

     if (d.ContainsKey( "C" ))

     {

         int p1 = d[ "C" ];

         Console.WriteLine(p1);

     }

}

2.在字典Dictionary<K, V>中删除指定键的元素


1

2

3

4

5

6

7

8

9

10

11

12

static void
Test( string [] args)

{

     Dictionary< string , int > d = new Dictionary< string , int >();

     d.Add( "C#" , 2);

     d.Add( "VB" , 1);

     d.Add( "C" , 0);

     d.Add( "C++" , -1);

     //删除键为“C”的元素

     d.Remove( "C" );

     //删除键为“VB”的元素

     d.Remove( "VB" );

}

3.使用ContainsValue查找值的存在


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

static void
Test( string [] args)

{

     Dictionary< string , int > d = new Dictionary< string , int >();

     d.Add( "C#" , 2);

     d.Add( "VB" , 1);

     d.Add( "C" , 0);

     d.Add( "C++" , -1);

     if (d.ContainsValue(1))

     {

         Console.WriteLine( "VB" );

     }

     if (d.ContainsValue(2))

     {

         Console.WriteLine( "C#" );

     }

     if (d.ContainsValue(0))

     {

         Console.WriteLine( "C" );

     }

     if (d.ContainsValue(-1))

     {

         Console.WriteLine( "C++" );

     }

}

4.KeyNotFoundException异常说明

如果你尝试读取字典中一个不存在的键,那么你会得到一个KeyNotFoundException。所有在读取一个键之前,你必须先使用ContainKey来核对键是否存在字典中。

5.排序字典SortedDictionary

对一个Dictionary<TKey, TValue>进行键排序可以直接用SortedDictionary, SortedDictionary<TKey, TValue>泛型类是检索运算复杂度为 O(log n) 的二叉搜索树,其中 n 是字典中的元素数。 就这一点而言,它与SortedList<TKey, TValue> 泛型类相似。 这两个类具有相似的对象模型,并且都具有 O(log n) 的检索运算复杂度。这两个类的区别在于内存的使用以及插入和移除元素的速度:SortedList<TKey, TValue> 使用的内存比SortedDictionary<TKey, TValue> 少,SortedDictionary<TKey, TValue> 可对未排序的数据执行更快的插入和移除操作:它的时间复杂度为 O(log n),而 SortedList<TKey, TValue> 为 O(n),如果使用排序数据一次性填充列表,则 SortedList<TKey, TValue> 比 SortedDictionary<TKey, TValue> 快。每个键/值对都可以作为KeyValuePair<TKey, TValue> 结构进行检索,或作为 DictionaryEntry 通过非泛型 IDictionary 接口进行检索。只要键用作 SortedDictionary<TKey, TValue> 中的键,它们就必须是不可变的。 SortedDictionary<TKey, TValue> 中的每个键必须是唯一的。 键不能为 null,但是如果值类型 TValue 为引用类型,该值则可以为空。SortedDictionary<TKey, TValue> 需要比较器实现来执行键比较。 可以使用一个接受 comparer 参数的构造函数来指定 IComparer<T> 泛型接口的实现;如果不指定实现,则使用默认的泛型比较器Comparer<T>.Default。 如果类型 TKey 实现 System.IComparable<T> 泛型接口,则默认比较器使用该实现。

总结

在这篇文章中,简要地介绍C#中的Dictionary<K,V>泛型字典的使用,通过它,你可以实现java和C++中Map集合类一样强大功能,通过本文的学习,你应该学会了Dictionary中如何添加键值、查找键值、删除元素等基本操作吧。

扫描二维码关注公众号,回复: 2476627 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_39081464/article/details/81025626