【问题标题】:I need to create a dictionary where each key can map to several values我需要创建一个字典,其中每个键都可以映射到多个值
【发布时间】:2014-04-16 10:56:01
【问题描述】:

我试图弄清楚如何创建类似于字典的东西,但每个键都可以映射到多个值。

基本上,我需要的是能够为同一个键分配多个值,而无需事先知道每个键对应多少个值。我还需要能够在多个场合向现有键添加值。如果我能检测到键 + 值组合何时已经存在,那就太好了。

程序应如何工作的示例:

list.Add(1,5);
list.Add(3,6);
list.Add(1,7);
list.Add(5,4);
list.Add(1,2);
list.Add(1,5);

理想情况下,这应该会生成一个像这样的表格:

1:5、7、2

3:6

5:4

C# 中是否有任何现有的结构可供我使用,还是我必须实现自己的类?实现类可能不是什么大问题,但我的时间有点短,所以如果我可以使用已经存在的东西会很好。

【问题讨论】:

  • 听起来像一个普通的字典,其中键是字符串或数字,值是列表。 :) 除了 .Add(),您必须在 value (即列表)而不是字典本身中添加一些内容。无论哪种方式,您的单元测试已经写在问题中是件好事。 :)

标签: c# dictionary key


【解决方案1】:

快速解决方案

正如您已经提到的,字典是最好的使用类型。您可以指定键类型和值类型以满足您的需要,在您的情况下,您需要一个 int 键和一个 List<int> 值。

这很容易创建:

Dictionary<int, List<int>> dictionary = new Dictionary<int, List<int>>();

挑战随之而来的是你如何添加记录,你不能简单地做Add(key, value),因为这会导致重复键的冲突。因此,您必须首先检索列表(如果存在)并添加到该列表中:

List<int> list = null;
if (dictionary.ContainsKey(key))
{
    list = dictionary[key];
}
else
{
    list = new List<int>();
    dictionary.Add(key, list);
}
list.Add(newValue);

首选解决方案

这显然是每次你想添加一个项目时使用的太多行,所以你想把它扔到一个辅助函数中,或者我更喜欢创建你自己的类来扩展字典的功能.像这样的:

class ListDictionary<T1, T2> : Dictionary<T1, List<T2>>
{
    public void Add(T1 key, T2 value)
    {
        if (this.ContainsKey(key))
        {
            this[key].Add(value);
        }
        else
        {
            List<T2> list = new List<T2>() { value };
            this.Add(key, list);
        }
    }

    public List<T2> GetValues(T1 key)
    {
        if(this.ContainsKey(key))
            return this[key];
        return null;
    }
}

然后您可以像最初想要的那样简单地使用它:

ListDictionary<int, int> myDictionary = new ListDictionary<int, int>();

myDictionary.Add(1,5);
myDictionary.Add(3,6);
//...and so on

然后获取所需键的值列表:

List<int> keyValues = myDictionary.GetValues(key);
//check if NULL before using, NULL means the key does not exist
//alternatively you can check if the key exists with if (myDictionary.ContainsKey(key))

【讨论】:

  • 这正是我所需要的。我最终使用了 HashSet 而不是 List ,现在它可以完美运行了。谢谢!
【解决方案2】:

您可以很容易地创建列表字典,例如

Dictionary<int, List<int>> dictionary = new Dictionary<int, List<int>>()

如果您已经创建了一个项目列表并希望使用不同的键将它们分成组,那么另一个替代方案是 Lookup 类。

【讨论】:

    【解决方案3】:
    Dictionary<int, List<int>> dictionary = new Dictionary<int, List<int>>();
    
    public void AddIfNotExistInDic(int key, int Value) {
        List<int> list = null;
        if (dictionary.ContainsKey(key)) {
            list = dictionary[key];
        }
        else {
            list = new List<int>();
            dictionary.Add(key, list);
        }
    
        if (!list.Contains(Value)) {
            list.Add(Value);
        }
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用Dictionary&lt;TKey, TValue&gt;TKey 将是intTValue 将是List&lt;int&gt;,您可以在列表中添加任意数量的元素,因为它会自动增长。

      Dictionary <int, List<int>> dic = new Dictionary<int, List<int>>();
      

      访问值的方式会发生变化,例如,您可以在字典中添加元素,例如

      void AddToYourCustomDictionary(int key, int someValue)
      {
         if(!dic.ContainsKey(key))
         {
            dic.Add(key, new List<int>());
             dic[key].Add(someValue);
         }
         else  
           dic[key].Add(someValue); //Adding element in existing key Value pair
      }
      

      要访问字典键中的元素 -> 值即列表,

      Console.WriteLine(dic[key][indexOfList]);
      

      【讨论】:

      • 然后你需要一些额外的逻辑(覆盖/替换添加)
      • dic[key] 如果密钥不存在会抛出错误,所以你不能像这样检查它是否为空
      猜你喜欢
      • 2018-03-19
      • 2021-05-10
      • 2021-05-01
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 2014-05-21
      • 2021-12-24
      • 1970-01-01
      相关资源
      最近更新 更多