F# - 可变字典



Dictionary<'TKey, 'TValue> 类是 F# 映射数据结构的可变模拟,包含许多相同的函数。

回顾 F# 中的映射章节,映射是一种特殊的集合,它将值与键关联。

创建可变字典

可变字典是使用 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]]

Dictionary(TKey,TValue) 类

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

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

属性

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

构造函数

构造函数 描述
Dictionary(TKey, TValue)() 初始化一个新的 Dictionary(TKey, TValue) 类实例,该实例为空,具有默认初始容量,并使用键类型的默认相等比较器。
Dictionary(TKey, TValue)(IDictionary(TKey, TValue)) 初始化一个新的 Dictionary(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) 类实例。

方法

方法 描述
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 获取与指定键关联的值。

示例

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"]
广告

© . All rights reserved.