C# .NET 字典中的重复键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/146204/
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
Duplicate keys in .NET dictionaries?
提问by
Are there any dictionary classes in the .NET base class library which allow duplicate keys to be used? The only solution I've found is to create, for example, a class like:
.NET 基类库中是否有允许使用重复键的字典类?我发现的唯一解决方案是创建一个类,例如:
Dictionary<string, List<object>>
But this is quite irritating to actually use. In Java, I believe a MultiMap accomplishes this, but cannot find an analog in .NET.
但这实际上使用起来很烦人。在 Java 中,我相信 MultiMap 可以实现这一点,但在 .NET 中找不到类似物。
采纳答案by Jon Skeet
If you're using .NET 3.5, use the Lookup
class.
如果您使用 .NET 3.5,请使用Lookup
该类。
EDIT: You generally create a Lookup
using Enumerable.ToLookup
. This does assume that you don't need to change it afterwards - but I typically find that's good enough.
编辑:您通常会创建一个Lookup
using Enumerable.ToLookup
. 这确实假设您之后不需要更改它 - 但我通常发现这已经足够了。
If that doesn'twork for you, I don't think there's anything in the framework which will help - and using the dictionary is as good as it gets :(
如果不为你工作,我不认为有一个在任何框架,这将有助于-并使用字典是尽善尽美:(
回答by MADMap
I think something like List<KeyValuePair<object, object>>
would do the Job.
我认为类似的事情List<KeyValuePair<object, object>>
可以完成这项工作。
回答by Ryan
Duplicate keys break the entire contract of the Dictionary. In a dictionary each key is unique and mapped to a single value. If you want to link an object to an arbitrary number of additional objects, the best bet might be something akin to a DataSet (in common parlance a table). Put your keys in one column and your values in the other. This is significantly slower than a dictionary, but that's your tradeoff for losing the ability to hash the key objects.
重复的键破坏了字典的整个契约。在字典中,每个键都是唯一的并映射到单个值。如果您想将一个对象链接到任意数量的附加对象,最好的办法可能是类似于 DataSet(通常是表)。将您的键放在一列中,将您的值放在另一列中。这比字典慢得多,但这是您失去散列关键对象能力的权衡。
回答by Nicholas Mancuso
Do you mean congruent and not an actual duplicate? Otherwise a hashtable wouldn't be able to work.
你的意思是一致的而不是实际的重复?否则哈希表将无法工作。
Congruent means that two separate keys can hash to the equivalent value, but the keys aren't equal.
Congruent 意味着两个单独的键可以散列到等效的值,但键不相等。
For example: say your hashtable's hash function was just hashval = key mod 3. Both 1 and 4 map to 1, but are different values. This is where your idea of a list comes into play.
例如:假设您的哈希表的哈希函数只是 hashval = key mod 3。1 和 4 都映射到 1,但是是不同的值。这就是您对列表的想法发挥作用的地方。
When you need to lookup 1, that value is hashed to 1, the list is traversed until the Key = 1 is found.
当您需要查找 1 时,将该值散列为 1,遍历列表直到找到 Key = 1。
If you allowed for duplicate keys to be inserted, you wouldn't be able to differentiate which keys map to which values.
如果您允许插入重复的键,您将无法区分哪些键映射到哪些值。
回答by Matt
If you are using strings as both the keys and the values, you can use System.Collections.Specialized.NameValueCollection, which will return an array of string values via the GetValues(string key) method.
如果您使用字符串作为键和值,您可以使用System.Collections.Specialized.NameValueCollection,它将通过 GetValues(string key) 方法返回一个字符串值数组。
回答by Matt
I just came across the PowerCollectionslibrary which includes, among other things, a class called MultiDictionary. This neatly wraps this type of functionality.
我刚刚遇到了PowerCollections库,其中包括一个名为 MultiDictionary 的类。这巧妙地包装了这种类型的功能。
回答by ckramer
The NameValueCollection supports multiple string values under one key (which is also a string), but it is the only example I am aware of.
NameValueCollection 支持一个键(也是一个字符串)下的多个字符串值,但它是我所知道的唯一示例。
I tend to create constructs similar to the one in your example when I run into situations where I need that sort of functionality.
当我遇到需要这种功能的情况时,我倾向于创建类似于您示例中的构造。
回答by TheSoftwareJedi
Very important note regarding use of Lookup:
关于使用 Lookup 的非常重要的注意事项:
You can create an instance of a Lookup(TKey, TElement)
by calling ToLookup
on an object that implements IEnumerable(T)
您可以Lookup(TKey, TElement)
通过调用ToLookup
实现的对象来创建 a 的实例IEnumerable(T)
There is no public constructor to create a new instance of a Lookup(TKey, TElement)
. Additionally, Lookup(TKey, TElement)
objects are immutable, that is, you cannot add or remove elements or keys from a Lookup(TKey, TElement)
object after it has been created.
没有公共构造函数来创建 a 的新实例Lookup(TKey, TElement)
。此外,Lookup(TKey, TElement)
对象是不可变的,也就是说,您不能在Lookup(TKey, TElement)
对象创建后添加或删除元素或键。
I'd think this would be a show stopper for most uses.
我认为这将是大多数用途的展示塞。
回答by TheSoftwareJedi
The List class actually works quite well for key/value collections containing duplicates where you would like to iterate over the collection. Example:
List 类实际上非常适用于包含重复项的键/值集合,您希望在其中迭代集合。例子:
List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
// add some values to the collection here
for (int i = 0; i < list.Count; i++)
{
Print(list[i].Key, list[i].Value);
}
回答by Dan
In answer to the original question. Something like Dictionary<string, List<object>>
is implemented in a class called MultiMap
in The Code Project
.
回答原来的问题。类似的东西Dictionary<string, List<object>>
是在一个名为MultiMap
The的类中实现的Code Project
。
You could find more info to the below link : http://www.codeproject.com/KB/cs/MultiKeyDictionary.aspx
您可以在以下链接中找到更多信息:http: //www.codeproject.com/KB/cs/MultiKeyDictionary.aspx