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中如何添加鍵值、查找鍵值、刪除元素等基本操作吧。