【问题标题】:Finding already existing value in Key Value pair在键值对中查找已经存在的值
【发布时间】:2013-01-06 13:00:23
【问题描述】:

我在键值对中存储一个字符串和 int 值。

var list = new List<KeyValuePair<string, int>>();

添加时我需要检查字符串(键)是否已经存在于列表中,如果存在我需要将其添加到值而不是添加新键。
如何查看和添加?

【问题讨论】:

  • 那你为什么不使用Dictionary呢?
  • 请注意,KeyValuePair&lt;string, int&gt; 是不可变的。每次要更新值时,您都必须删除现有条目并创建一个新条目。这就是为什么Dictionary 比答案中给出的要好得多。
  • 谢谢,我要去字典了。

标签: c# .net key-value


【解决方案1】:

您的需求准确描述了Dictionarys 的设计?

Dictionary<string, string> openWith = 
        new Dictionary<string, string>();

// Add some elements to the dictionary. There are no  
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");

// If a key does not exist, setting the indexer for that key 
// adds a new key/value pair.
openWith["doc"] = "winword.exe";

【讨论】:

  • 地图是一个非常Java的词;p
  • @Olivarsham 检查我的答案,你想要Dictionary
【解决方案2】:

使用字典。 Dictionary in C#,我建议你阅读这篇文章Dictonary in .net

Dictionary<string, int> dictionary =
        new Dictionary<string, int>();
    dictionary.Add("cat", 2);
    dictionary.Add("dog", 1);
    dictionary.Add("llama", 0);
    dictionary.Add("iguana", -1);

检查。使用 ContainsKey ContainsKey

if (dictionary.ContainsKey("key"))
    dictionary["key"] = dictionary["key"] + yourValue;

【讨论】:

    【解决方案3】:

    您可以使用 Dictionary 代替 List 并检查它是否为 contains key 然后将新值添加到现有键中

    int newValue = 10;
    Dictionary<string, int> dictionary = new Dictionary<string, int>();
    if (dictionary.ContainsKey("key"))
        dictionary["key"] = dictionary["key"] + newValue;
    

    【讨论】:

    • 请注意:字典不允许按索引访问。如果这是一个要求(就像我的情况一样),那么这不是一个可行的解决方案。
    • @Dan, Dictionary 确实允许按索引访问,例如 dictionary["myKey"] 我不确定您的意思。
    • 但另一方面,字典不支持具有不同值的相同键。
    • @Dan,如果需要按位置获取条目,可以使用 ElementAt[myInt]。
    • @BH 谢谢,下次需要的时候我会试试的。
    【解决方案4】:

    如果你需要使用列表,你必须foreach列表,并寻找键。 很简单,你可以使用hashtable。

    【讨论】:

      【解决方案5】:

      当然,在您的情况下,字典更可取。您不能修改 KeyValue&lt;string,int&gt; 类的值,因为它是不可变的。

      但即使你仍然想使用List&lt;KeyValuePair&lt;string, int&gt;&gt;();。您可以使用IEqualityComparer&lt;KeyValuePair&lt;string, int&gt;&gt;。代码会是这样的。

      public class KeyComparer : IEqualityComparer<KeyValuePair<string, int>>
      {
      
          public bool Equals(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
          {
              return x.Key.Equals(y.Key);
          }
      
          public int GetHashCode(KeyValuePair<string, int> obj)
          {
              return obj.Key.GetHashCode();
          }
      }
      

      并在 Contains like 中使用它

      var list = new List<KeyValuePair<string, int>>();
              string checkKey = "my string";
              if (list.Contains(new KeyValuePair<string, int>(checkKey, int.MinValue), new KeyComparer()))
              {
                  KeyValuePair<string, int> item = list.Find((lItem) => lItem.Key.Equals(checkKey));
                  list.Remove(item);
                  list.Add(new KeyValuePair<string, int>("checkKey", int.MinValue));// add new value
              }
      

      这听起来不太好。

      希望这些信息对您有所帮助..

      【讨论】:

        【解决方案6】:

        对于必须使用 List 的任何人(对我来说就是这种情况,因为它可以做 Dictionary 不做的事情),您可以只使用 lambda 表达式来查看 List 是否包含 Key:

        list.Any(l => l.Key == checkForKey);
        

        【讨论】:

          猜你喜欢
          • 2011-05-17
          • 1970-01-01
          • 1970-01-01
          • 2012-05-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-22
          相关资源
          最近更新 更多