C# 在键值对中查找已经存在的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14473101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding already existing value in Key Value pair
提问by Olivarsham
I am storing a string and int value in Key value pair.
我在键值对中存储一个字符串和一个 int 值。
var list = new List<KeyValuePair<string, int>>();
While adding i need to check if string(Key) already exists in list, if exists i need to add it to Value instead of adding new key.
How to check and add?
添加时我需要检查字符串(键)是否已存在于列表中,如果存在,我需要将其添加到值而不是添加新键。
如何查看和添加?
采纳答案by Habib
Instead of List you can use Dictionaryand check if it contains keythen add the new value to the existing key
您可以使用Dictionary代替 List并检查它是否包含键,然后将新值添加到现有键
int newValue = 10;
Dictionary<string, int> dictionary = new Dictionary<string, int>();
if (dictionary.ContainsKey("key"))
dictionary["key"] = dictionary["key"] + newValue;
回答by Karthik T
Your needs exactly describe the design of Dictionary
s?
您的需求准确地描述了Dictionary
s的设计吗?
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";
回答by Ravi Gadag
use dictonary. Dictionary in C#and I suggest you to read this post Dictonary in .net
使用字典。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);
to check. use ContainsKey ContainsKey
去检查。使用 ContainsKey ContainsKey
if (dictionary.ContainsKey("key"))
? ? dictionary["key"] = dictionary["key"] + yourValue;
回答by Jhonny
If you need use the list,you must foreach the list,and look for the keys. Simplely,you can use hashtable.
如果您需要使用列表,您必须foreach列表,并寻找键。简单地说,您可以使用哈希表。
回答by D J
For sure, dictionary is preferable in your case. You can not modify the Value of KeyValue<string,int>
class as it is Immutable.
当然,在您的情况下,字典更可取。您不能修改KeyValue<string,int>
类的值,因为它是不可变的。
But even if you still want to use List<KeyValuePair<string, int>>();
. You can use IEqualityComparer<KeyValuePair<string, int>>
. Code will be like.
但即使您仍然想使用List<KeyValuePair<string, int>>();
. 您可以使用IEqualityComparer<KeyValuePair<string, int>>
. 代码会像。
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();
}
}
And use it in 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
}
which does not sounds good way.
这听起来不是个好方法。
hope this info helps..
希望这些信息有帮助..
回答by WATYF
For anyone who has to use a List (which was the case for me, since it does things Dictionary doesn't), you can just use a lambda expression to see if the List contains the Key:
对于必须使用 List 的任何人(对我来说就是这种情况,因为它可以做 Dictionary 没有的事情),您可以只使用 lambda 表达式来查看 List 是否包含键:
list.Any(l => l.Key == checkForKey);