📅  最后修改于: 2020-11-21 06:54:04             🧑  作者: Mango
Dictionary <‘TKey,’TValue>类是F#地图数据结构的可变模拟,并且包含许多相同的功能。
从F#的“地图”一章中概述,地图是一种特殊的集合,将值与键相关联。
使用new关键字并调用列表的构造函数来创建可变字典。以下示例演示了这一点-
open System.Collections.Generic
let dict = new Dictionary()
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)类表示键和值的集合。
下表提供了List(T)类的属性,构造函数和方法-
Property | Description |
---|---|
Comparer | Gets the IEqualityComparer(T) that is used to determine equality of keys for the dictionary. |
Count | Gets the number of key/value pairs contained in the Dictionary(TKey, TValue). |
Item | Gets or sets the value associated with the specified key. |
Keys | Gets a collection containing the keys in the Dictionary(TKey, TValue). |
Values | Gets a collection containing the values in the Dictionary(TKey, TValue). |
Constructors | Description |
---|---|
Dictionary(TKey, TValue)() | Initializes a new instance of the Dictionary(TKey, TValue) class that is empty, has the default initial capacity, and uses the default equality comparer for the key type. |
Dictionary(TKey, TValue)(IDictionary(TKey, TValue)) | Initializes a new instance of the Dictionary(TKey, TValue) class that contains elements copied from the specified IDictionary(TKey, TValue) and uses the default equality comparer for the key type. |
Dictionary(TKey, TValue)(IEqualityComparer(TKey)) | Initializes a new instance of the Dictionary(TKey, TValue) class that is empty, has the default initial capacity, and uses the specified IEqualityComparer(T). |
Dictionary(TKey, TValue)(Int32) | Initializes a new instance of the Dictionary(TKey, TValue) class that is empty, has the specified initial capacity, and uses the default equality comparer for the key type. |
Dictionary(TKey, TValue)(IDictionary(TKey, TValue), IEqualityComparer(TKey)) | Initializes a new instance of the Dictionary(TKey, TValue) class that contains elements copied from the specified IDictionary(TKey, TValue) and uses the specified IEqualityComparer(T). |
Dictionary(TKey, TValue)(Int32, IEqualityComparer(TKey)) | Initializes a new instance of the Dictionary(TKey, TValue) class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer(T). |
Dictionary(TKey, TValue)(SerializationInfo, StreamingContext) | Initializes a new instance of the ictionary(TKey, TValue) class with serialized data. |
Method | Description |
---|---|
Add | Adds the specified key and value to the dictionary. |
Clear | Removes all keys and values from the Dictionary(TKey, TValue). |
ContainsKey | Determines whether the Dictionary(TKey, TValue) contains the specified key. |
ContainsValue | Determines whether the Dictionary(TKey, TValue) contains a specific value. |
Equals(Object) | Determines whether the specified object is equal to the current object. (Inherited from Object.) |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
GetEnumerator | Returns an enumerator that iterates through the Dictionary(TKey, TValue). |
GetHashCode | Serves as the default hash function. (Inherited from Object.) |
GetObjectData | Implements the System.Runtime.Serialization.ISerializable interface and returns the data needed to serialize the Dictionary(TKey, TValue)instance. |
GetType | Gets the Type of the current instance. (Inherited from Object.) |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
OnDeserialization | Implements the System.Runtime.Serialization.ISerializable interface and raises the deserialization event when the deserialization is complete. |
Remove | Removes the value with the specified key from the Dictionary(TKey, TValue). |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
TryGetValue | Gets the value associated with the specified key. |
open System.Collections.Generic
let dict = new Dictionary()
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"]