目录

F# - 可变字典( Mutable Dictionary)

Dictionary《'TKey, 'TValue》类是F#地图数据结构的可变模拟,包含许多相同的功能。

从F#中的Map章节重述,地图是一种特殊的集合,它将值与键相关联。

创建一个可变字典

使用new关键字创建可变字典并调用列表的构造函数。 以下示例演示了这一点 -

open System.Collections.Generic
let dict = new Dictionary<string, string>()
dict.Add("1501", "Zara Ali")
dict.Add("1502","Rishita Gupta")
dict.Add("1503","Robin Sahoo")
dict.Add("1504","Gillian Megan")
printfn "Dictionary - students: %A" dict

编译并执行程序时,它会产生以下输出 -

Dictionary - students: seq
[[1501, Zara Ali]; [1502, Rishita Gupta]; [1503, Robin Sahoo];
[1504, Gillian Megan]]

The Dictionary(TKey,TValue) Class

Dictionary(TKey,TValue)类表示键和值的集合。

下表提供了List(T)类的属性,构造函数和方法 -

属性 (Properties)

属性 描述
Comparer 获取用于确定字典的键相等性的IEqualityComparer(T)。
Count 获取Dictionary(TKey,TValue)中包含的键/值对的数量。
Item 获取或设置与指定键关联的值。
Keys 获取包含Dictionary(TKey,TValue)中的键的集合。
Values 获取包含Dictionary(TKey,TValue)中的值的集合。

构造函数 (Constructors)

构造函数 描述
Dictionary(TKey, TValue)() 初始化一个新的Dictionary(TKey, TValue)类实例,该类具有默认的初始容量,并使用默认的相等比较器作为键类型。
Dictionary(TKey, TValue)(IDictionary(TKey, TValue)) 初始化Dictionary(TKey, TValue)类的新实例,该类包含从指定的IDictionary(TKey, TValue)复制的元素IDictionary(TKey, TValue)并使用默认的相等比较器作为键类型。
Dictionary(TKey, TValue)(IEqualityComparer(TKey)) 初始化一个新的Dictionary(TKey, TValue)类实例,它是空的,具有默认的初始容量,并使用指定的IEqualityComparer(T).
Dictionary(TKey, TValue)(Int32) 初始化一个新的Dictionary(TKey, TValue)类实例,它是空的,具有指定的初始容量,并使用默认的相等比较器作为键类型。
Dictionary(TKey, TValue)(IDictionary(TKey, TValue), IEqualityComparer(TKey)) 初始化Dictionary(TKey, TValue)类的新实例,该类包含从指定的IDictionary(TKey, TValue)复制的元素并使用指定的IEqualityComparer(T).
Dictionary(TKey, TValue)(Int32, IEqualityComparer(TKey)) 初始化Dictionary(TKey, TValue)类的新实例,该类是空的,具有指定的初始容量,并使用指定的IEqualityComparer(T).
Dictionary(TKey, TValue)(SerializationInfo, StreamingContext) 使用序列化数据初始化ictionary(TKey, TValue)类的新实例。

方法 (Methods)

方法 描述
Add 将指定的键和值添加到字典中。
Clear 从Dictionary(TKey,TValue)中删除所有键和值。
ContainsKey 确定Dictionary(TKey,TValue)是否包含指定的键。
ContainsValue 确定Dictionary(TKey,TValue)是否包含特定值。
Equals(Object) 确定指定的对象是否等于当前对象。 (继承自Object。)
Finalize 允许对象在垃圾回收回收之前尝试释放资源并执行其他清理操作。 (继承自Object。)
GetEnumerator 返回一个遍历Dictionary(TKey,TValue)的枚举器。
GetHashCode 用作默认哈希函数。 (继承自Object。)
GetObjectData 实现System.Runtime.Serialization.ISerializable接口并返回序列化Dictionary(TKey,TValue)实例所需的数据。
GetType 获取当前实例的Type。 (继承自Object。)
MemberwiseClone 创建当前Object的浅表副本。 (继承自Object。)
OnDeserialization 实现System.Runtime.Serialization.ISerializable接口,并在反序列化完成时引发反序列化事件。
Remove 从Dictionary(TKey,TValue)中删除指定键的值。
ToString 返回表示当前对象的字符串。 (继承自Object。)
TryGetValue 获取与指定键关联的值。

例子 (Example)

open System.Collections.Generic
let dict = new Dictionary<string, string>()
dict.Add("1501", "Zara Ali")
dict.Add("1502","Rishita Gupta")
dict.Add("1503","Robin Sahoo")
dict.Add("1504","Gillian Megan")
printfn "Dictionary - students: %A" dict
printfn "Total Number of Students: %d" dict.Count
printfn "The keys: %A" dict.Keys
printf"The Values: %A" dict.Values

编译并执行程序时,它会产生以下输出 -

Dictionary - students: seq
[[1501, Zara Ali]; [1502, Rishita Gupta]; [1503, Robin Sahoo];
[1504, Gillian Megan]]
Total Number of Students: 4
The keys: seq ["1501"; "1502"; "1503"; "1504"]
The Values: seq ["Zara Ali"; "Rishita Gupta"; "Robin Sahoo"; "Gillian Megan"]
↑回到顶部↑
WIKI教程 @2018