📅  最后修改于: 2023-12-03 14:59:41.653000             🧑  作者: Mango
#c# 具有多个值的字典
在C#中,字典是一种常用的数据结构,它提供了一种将键值对进行快速检索的方式。但是,有时候我们需要在字典中存储多个值,而不是只有一个值。那么怎样实现这种具有多个值的字典呢?C#中有三种常用的方式。
Dictionary<TKey, TValue>
是C#中常用的字典类型。要实现具有多个值的字典,可以使用 Dictionary<TKey, List<TValue>>
类型,其中List<TValue>
表示值的集合。以下是示例代码:
Dictionary<string, List<int>> multiValueDict = new Dictionary<string, List<int>>();
multiValueDict.Add("key1", new List<int>() { 1, 2 });
multiValueDict.Add("key2", new List<int>() { 3, 4, 5 });
foreach (KeyValuePair<string, List<int>> kvp in multiValueDict)
{
Console.WriteLine(kvp.Key + ": ");
foreach (int val in kvp.Value)
{
Console.WriteLine(val);
}
}
在上述示例代码中,我们定义了一个Dictionary<string, List<int>>
类型的字典 multiValueDict
,使用 Add
方法向其添加了两个键值对。然后,使用foreach循环遍历字典中的键值对,并打印每个键值对的键和值。
Lookup<TKey, TValue>
是C#中用于表示具有多个值的字典的另一种类型。以下是示例代码:
string[] keys = { "key1", "key2" };
int[][] values = { new int[] { 1, 2 }, new int[] { 3, 4, 5 } };
Lookup<string, int> multiValueDict = keys
.Select((k, i) => new { Key = k, Value = values[i] })
.ToLookup(x => x.Key, x => x.Value);
foreach (IGrouping<string, int[]> group in multiValueDict)
{
Console.WriteLine(group.Key + ": ");
foreach (int val in group.SelectMany(x => x))
{
Console.WriteLine(val);
}
}
在上述示例代码中,我们定义了一个字符串类型的数组 keys
和一个二维整数数组values
。然后,我们使用 Select
方法将 keys
和 values
数组中的元素合并成一个匿名类型,并使用 ToLookup
方法将它们转换为一个 Lookup<string, int>
类型的字典。最后,foreach 循环遍历字典中的键值对,并打印每个键值对的键和值,其中多个值被展平成了一个序列。
MultiDictionary<TKey, TValue>
是C#提供的一个第三方的多值字典类,它实现了 IDictionary<TKey, ICollection<TValue>>
接口,可以很方便地实现具有多个值的字典。以下是示例代码:
MultiDictionary<string, int> multiValueDict = new MultiDictionary<string, int>();
multiValueDict.Add("key1", 1);
multiValueDict.Add("key1", 2);
multiValueDict.Add("key2", 3);
multiValueDict.Add("key2", 4);
multiValueDict.Add("key2", 5);
foreach (KeyValuePair<string, ICollection<int>> kvp in multiValueDict)
{
Console.WriteLine(kvp.Key + ": ");
foreach (int val in kvp.Value)
{
Console.WriteLine(val);
}
}
在上述示例代码中,我们创建了一个 MultiDictionary<string, int>
类型的多值字典 multiValueDict
,然后使用 Add
方法向其中添加了多个键值对。最后,我们使用foreach循环遍历字典中的键值对,并打印每个键值对的键和所有的值。
以上三种方式都可以实现具有多个值的字典,具体使用哪种方式取决于具体的场景。以下是示例代码的markdown代码片段:
```csharp
Dictionary<string, List<int>> multiValueDict = new Dictionary<string, List<int>>();
multiValueDict.Add("key1", new List<int>() { 1, 2 });
multiValueDict.Add("key2", new List<int>() { 3, 4, 5 });
foreach (KeyValuePair<string, List<int>> kvp in multiValueDict)
{
Console.WriteLine(kvp.Key + ": ");
foreach (int val in kvp.Value)
{
Console.WriteLine(val);
}
}
string[] keys = { "key1", "key2" };
int[][] values = { new int[] { 1, 2 }, new int[] { 3, 4, 5 } };
Lookup<string, int> multiValueDict = keys
.Select((k, i) => new { Key = k, Value = values[i] })
.ToLookup(x => x.Key, x => x.Value);
foreach (IGrouping<string, int[]> group in multiValueDict)
{
Console.WriteLine(group.Key + ": ");
foreach (int val in group.SelectMany(x => x))
{
Console.WriteLine(val);
}
}
MultiDictionary<string, int> multiValueDict = new MultiDictionary<string, int>();
multiValueDict.Add("key1", 1);
multiValueDict.Add("key1", 2);
multiValueDict.Add("key2", 3);
multiValueDict.Add("key2", 4);
multiValueDict.Add("key2", 5);
foreach (KeyValuePair<string, ICollection<int>> kvp in multiValueDict)
{
Console.WriteLine(kvp.Key + ": ");
foreach (int val in kvp.Value)
{
Console.WriteLine(val);
}
}