如何测试 C# Hashtable 是否包含特定的键/值对?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/663150/
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
How to test if a C# Hashtable contains a specific key/value pair?
提问by Brian
I'm storing a bunch of supposedly-unique item IDs as a key and the file locations as the value in a hash table while traversing a table. While I am running through it, I need to make sure that they key/location pair is unique or throw an error message. I have the hashtable set up and am loading the values, but am not sure what to test:
在遍历表时,我将一堆据称是唯一的项目 ID 存储为键,并将文件位置存储为哈希表中的值。当我运行它时,我需要确保它们的键/位置对是唯一的或抛出错误消息。我设置了哈希表并加载了值,但不确定要测试什么:
Hashtable check_for_duplicates = new HashTable();
foreach (object item in items)
{
if (check_for_duplicates.ContainsKey(item["ItemID"]) &&
//what goes here? Would be contains item["Path"] as the value for the key)
{
//throw error
}
}
采纳答案by Chris Doggett
Try this:
尝试这个:
Hashtable check_for_duplicates = new HashTable();
foreach (object item in items)
{
if (check_for_duplicates.ContainsKey(item["ItemID"]) &&
check_for_duplicates[item["ItemID"]].Equals(item["Path"]))
{
//throw error
}
}
Also, if you're using .NET 2.0 or higher, consider using Generics, like this:
此外,如果您使用 .NET 2.0 或更高版本,请考虑使用泛型,如下所示:
List<Item> items; // Filled somewhere else
// Filters out duplicates, but won't throw an error like you want.
HashSet<Item> dupeCheck = new HashSet<Item>(items);
items = dupeCheck.ToList();
Actually, I just checked, and it looks like HashSet is .NET 3.5 only. A Dictionary would be more appropriate for 2.0:
实际上,我刚刚检查过,看起来 HashSet 只是 .NET 3.5。字典更适合 2.0:
Dictionary<int, string> dupeCheck = new Dictionary<int, string>();
foreach(Item item in items) {
if(dupeCheck.ContainsKey(item.ItemID) &&
dupeCheck[item.ItemID].Equals(item.Path)) {
// throw error
}
else {
dupeCheck[item.ItemID] = item.Path;
}
}
回答by Mehrdad Afshari
If you were using Dictionary
instead, the TryGetValue
method would help. I don't think there is a really better way for the pretty much deprecated Hashtable
class.
如果您正在使用Dictionary
,该TryGetValue
方法会有所帮助。对于几乎已弃用的Hashtable
课程,我认为没有更好的方法。
object value;
if (dic.TryGetValue("key", out value) && value == thisValue)
// found duplicate
回答by Ken Browning
if (check_for_duplicates.ContainsKey(item["ItemID"]) &&
check_for_duplicates[item["ItemID"]] == item["Path"])
{
//throw error
}
回答by Ian
It kinda depends what the items array is... you'll want something like:
这有点取决于 items 数组是什么......你会想要这样的东西:
check_for_duplicates.ContainsValue(item["Path"]);
Assuming that the item is some form of lookup. Really you need to be casting item, or using a type system to actually access any values via an index.
假设该项目是某种形式的查找。实际上,您需要强制转换项目,或使用类型系统通过索引实际访问任何值。
回答by David Basarab
ContainsKey is the best method.
containsKey 是最好的方法。
If you aren't forced to use .NET 1.1 I would use the Dictionary introduced in .NET 2.0.
如果您不是被迫使用 .NET 1.1,我将使用 .NET 2.0 中引入的 Dictionary。
It is much better than a Hashtable from performance and is strongly typed.
它在性能上比 Hashtable 好得多,并且是强类型的。
Dictionary<string, int> betterThanAHash = new Dictionary<string, int>();
betterThanAHash.ContainsKey("MyKey");
回答by Matt Grande
Hashtable check_for_duplicates = new HashTable();
foreach (object item in items)
{
if (check_for_duplicates.ContainsKey(item["ItemID"]) && check_for_duplicates[item["ItemID"]] == item["Path"])
{
//throw error
}
}
I do believe this is what you're looking for.
我相信这就是你要找的。
EDIT - Looks like I was beaten to the punch :P
编辑 - 看起来我被打了:P
回答by GrahamS
Why not use a Dictionary
instead?
为什么不使用 aDictionary
呢?
That will throw an ArgumentException if you try to Add
a key that already exists in the Dictionary
.
如果您尝试Add
使用已存在于Dictionary
.
That way you can catch the duplicate at the moment it is being added, rather than performing a check_for_duplicates
test later.
这样您就可以在添加副本时捕获重复项,而不是check_for_duplicates
稍后执行测试。
回答by Jason Stelzer
You didn't say what version of things you were using. Is there a reason you must use a Hashtable vs a HashSet? You would have no need to check for duplicates if your data structure didn't allow them. See also:
你没有说你使用的是什么版本的东西。是否有理由必须使用 Hashtable 与 HashSet?如果您的数据结构不允许,您就无需检查重复项。也可以看看:
http://www.vcskicks.com/csharp_data_structures2.html
http://www.vcskicks.com/csharp_data_structures2.html
Other than that, the question of how to accomplish the same thing in Hashtable has already been answered here. I'm just pointing out that you don't need to do all the pathological checking if you forbid it in the first place.
除此之外,如何在Hashtable中完成同样的事情的问题已经在这里得到了解答。我只是指出,如果您首先禁止它,则不需要进行所有病理检查。