Dictionary <'TKey,'TValue>类是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)类表示键和值的集合。
属性 | 说明 |
---|---|
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)复制的元素,并使用默认的相等比较器作为键类型。 |
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 | 获取当前的类型。 (继承自Object。) |
MemberwiseClone | 创建当前对象的浅表副本。 (继承自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"]
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)