一、Dictionary
Dictionary<TKey, TValue> 泛型类提供了键值对的映射。通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictionary<TKey, TValue> 类是作为一个哈希表来实现的。检索速度取决于为 TKey 指定的类型的哈希算法的质量。TValue可以是值类型,数组,类或其他。
Dictionary是一种变种的HashTable,它采用一种分离链接散列表的数据结构来解决哈希冲突的问题。命名空间System.Collection.Generic 。
Dictionary属性和方法
1、Dictionary一些常用的属性
属性 | 描述 |
---|---|
Comparer | 获取用于确定字典中的键是否相等的 IEqualityComparer |
Count | 获取包含在 Dictionary<TKey, TValue> 中的键/值对的数目 |
Item | 获取或设置与指定的键相关联的值 |
Keys | 获取包含 Dictionary<TKey, TValue> 中的键的集合 |
Values | 获取包含 Dictionary<TKey, TValue> 中的值的集合 |
2、Dictionary一些常用的方法
方法 | 描述 |
---|---|
Add | 将指定的键和值添加到字典中 |
Clear | 从 Dictionary<TKey, TValue> 中移除所有的键和值 |
ContainsKey | 确定 Dictionary<TKey, TValue> 是否包含指定的键 |
ContainsValue | 确定 Dictionary<TKey, TValue> 是否包含特定值 |
Equals(Object) | 确定指定的 Object 是否等于当前的 Object |
Finalize | 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作 |
GetEnumerator | 返回循环访问 Dictionary<TKey, TValue> 的枚举器 |
GetHashCode | 用作特定类型的哈希函数 |
GetObjectData | 实现 System.Runtime.Serialization.ISerializable 接口,并返回序列化 Dictionary<TKey, TValue> 实例所需的数据 |
GetType | 获取当前实例的 Type |
MemberwiseClone | 创建当前 Object 的浅表副本 |
OnDeserialization | 实现 System.Runtime.Serialization.ISerializable 接口,并在完成反序列化之后引发反序列化事件 |
Remove | 从 Dictionary<TKey, TValue> 中移除所指定的键的值 |
ToString | 返回表示当前对象的字符串 |
TryGetValue | 获取与指定的键相关联的值 |
实例
using System;using System.Collections;using System.Collections.Generic;namespace WebApp{class Program{static void Main(string[] args){Dictionary<string, string> myDic = new Dictionary<string, string>();//插入myDic.Add(\"1\", \"joye.net\");myDic.Add(\"2\", \"joye.net2\");myDic.Add(\"3\", \"joye.net3\");//key 存在try{myDic.Add(\"1\", \"1joye.net\");}catch{Console.WriteLine(\"Key = \\\"1\\\" already exists.\");}//取值Console.WriteLine(\"key = \\\"2\\\", value = {0}.\", myDic[\"2\"]);//修改myDic[\"2\"] = \"http://www.cnblogs.com/yinrq/\";myDic[\"4\"] = \"joye.net4\"; //修改的key不存在则新增Console.WriteLine(\"key = \\\"2\\\", value = {0}.\", myDic[\"2\"]);Console.WriteLine(\"key = \\\"4\\\", value = {0}.\", myDic[\"4\"]);//判断key是否存在if (!myDic.ContainsKey(\"5\")){myDic.Add(\"5\", \"joye.net5\");Console.WriteLine(\"key = \\\"5\\\": {0}\", myDic[\"5\"]);}//移除myDic.Remove(\"1\");if (!myDic.ContainsKey(\"1\")){Console.WriteLine(\"Key \\\"1\\\" is not found.\");}//foreach 取值foreach (var item in myDic){Console.WriteLine(\"Key = {0}, Value = {1}\", item.Key, item.Value);}//遍历字典//foreach (KeyValuePair<string, string> kvp in myDic)//{//Console.WriteLine(\"Key = {0}, Value = {1}\", kvp.Key, kvp.Value);//}//所有的值foreach (var item in myDic.Values){Console.WriteLine(\"Value = {0}\",item);}//所有的keyforeach (var item in myDic.Keys){Console.WriteLine(\"Key = {0}\", item);}Console.ReadKey();}}}
结果
Key = \"1\" already exists.key = \"2\", value = Dictionary.net2.key = \"2\". value = DictionaryNew.net2.key = \"4\". value = Dictionary.net4.key = \"5\": Dictionary.net5Key \"1\" is not found.Key = 2. Value = DictionaryNew.net2Key = 3. Value = Dictionary.net3Key = 4. Value = Dictionary.net4Key = 5. Value — Dictionary.net5Value = DictionaryNew.net2Value = Dictionary.net3Value = Dictionary.net4Value = Dictionary.netSKey = 2Key = 3Key = 4Key = 5
二、ConcurrentDictionary
ConcurrentDictionary<TKey,TValue> 类表示可由多个线程同时访问的键/值对的线程安全集合。命名空间:System.Collections.Concurrent。
ConcurrentDictionary属性和方法
1、ConcurrentDictionary的常用属性
属性 | 描述 |
---|---|
Count | 获取包含在 ConcurrentDictionary<TKey,TValue> 中的键/值对的数目。 |
IsEmpty | 获取一个值,该值指示 ConcurrentDictionary<TKey,TValue> 是否为空。 |
Item[TKey] | 获取或设置与指定的键关联的值。 |
Keys | 获得一个包含 Dictionary<TKey,TValue> 中的键的集合。 |
Values | 获取包含 Dictionary<TKey,TValue> 中的值的集合。 |
2、ConcurrentDictionary的常用方法
方法 | 描述 |
---|---|
AddOrUpdate(TKey, Func<TKey,TValue>, Func<TKey,TValue,TValue>) | 如果该键不存在,则使用第一个函数将键/值对添加到 ConcurrentDictionary<TKey,TValue>;如果该键已存在,则使用第二个函数更新 ConcurrentDictionary<TKey,TValue> 中的键/值对。 |
AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>) | 如果该键不存在,则将键/值对添加到 ConcurrentDictionary<TKey,TValue> 中;如果该键已经存在,则通过使用指定函数更新 ConcurrentDictionary<TKey,TValue> 中的键/值对。 |
Clear() | 将所有键和值从 ConcurrentDictionary<TKey,TValue> 中移除。 |
ContainsKey(TKey) | 确定是否 ConcurrentDictionary<TKey,TValue> 包含指定键。 |
Equals(Object) | 确定指定的对象是否等于当前对象。 |
GetEnumerator() | 返回遍历 ConcurrentDictionary<TKey,TValue> 的枚举器 |
GetHashCode() | 作为默认哈希函数。 |
GetOrAdd(TKey, Func<TKey,TValue>) | 如果该键不存在,则通过使用指定的函数将键/值对添加到 ConcurrentDictionary<TKey,TValue> 中。 如果该键存在,则返回新值或现有值。 |
GetOrAdd(TKey, TValue) | 如果该键不存在,则将键/值对添加到 ConcurrentDictionary<TKey,TValue> 中。 如果该键存在,则返回新值或现有值。 |
public KeyValuePair<TKey,TValue>[] ToArray() | 将 ConcurrentDictionary<TKey,TValue> 中存储的键和值对复制到新数组中。 |
ToString() | 返回表示当前对象的字符串。 |
TryAdd(TKey, TValue) | 尝试将指定的键和值添加到 ConcurrentDictionary<TKey,TValue> 中。 |
TryGetValue(TKey, TValue) | 尝试从 ConcurrentDictionary<TKey,TValue> 获取与指定的键关联的值。 |
TryRemove(TKey, TValue) | 尝试从 ConcurrentDictionary<TKey,TValue> 中移除并返回具有指定键的值。 |
TryUpdate(TKey, TValue, TValue) | 如果具有 key 的现有值等于 comparisonValue,则将与 key 关联的值更新为 newValue。 |