C# 键值对数据结构的最佳实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8800/
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
Best implementation for Key Value Pair Data Structure?
提问by Bernard
So I've been poking around with C# a bit lately, and all the Generic Collections have me a little confused. Say I wanted to represent a data structure where the head of a tree was a key value pair, and then there is one optional list of key value pairs below that (but no more levels than these). Would this be suitable?
所以我最近一直在研究 C#,所有的通用集合让我有点困惑。假设我想表示一个数据结构,其中树的头部是一个键值对,然后在它下面有一个可选的键值对列表(但不超过这些级别)。这会合适吗?
public class TokenTree
{
public TokenTree()
{
/* I must admit to not fully understanding this,
* I got it from msdn. As far as I can tell, IDictionary is an
* interface, and Dictionary is the default implementation of
* that interface, right?
*/
SubPairs = new Dictionary<string, string>();
}
public string Key;
public string Value;
public IDictionary<string, string> SubPairs;
}
It's only really a simple shunt for passing around data.
这只是传递数据的一个简单分流器。
采纳答案by Adam Haile
There is an actual Data Type called KeyValuePair, use like this
有一个实际的数据类型叫做 KeyValuePair,像这样使用
KeyValuePair<string, string> myKeyValuePair = new KeyValuePair<string,string>("defaultkey", "defaultvalue");
回答by Lasse V. Karlsen
Dictionary Classis exactly what you want, correct.
Dictionary Class正是你想要的,正确的。
You can declare the field directly as Dictionary, instead of IDictionary, but that's up to you.
您可以直接将该字段声明为 Dictionary,而不是 IDictionary,但这取决于您。
回答by Coincoin
There is a KeyValuePair built-in type. As a matter of fact, this is what the IDictionary is giving you access to when you iterate in it.
有一个 KeyValuePair 内置类型。事实上,这就是 IDictionary 在您迭代时为您提供的访问权限。
Also, this structure is hardly a tree, finding a more representative name might be a good exercise.
此外,这种结构几乎不是一棵树,找到一个更具代表性的名称可能是一个很好的练习。
回答by Jon Limjap
One possible thing you could do is use the Dictionary object straight out of the box and then just extend it with your own modifications:
您可以做的一件事是直接使用 Dictionary 对象,然后使用您自己的修改对其进行扩展:
public class TokenTree : Dictionary<string, string>
{
public IDictionary<string, string> SubPairs;
}
This gives you the advantage of not having to enforce the rules of IDictionary for your Key (e.g., key uniqueness, etc).
这为您提供了不必为您的密钥强制执行 IDictionary 规则(例如,密钥唯一性等)的优势。
And yup you got the concept of the constructor right :)
是的,你对构造函数的概念有正确的认识:)
回答by Lasse V. Karlsen
@Jay Mooney: A generic Dictionary class in .NET is actually a hash table, just with fixed types.
@ Jay Mooney:.NET 中的通用 Dictionary 类实际上是一个哈希表,只是具有固定类型。
The code you've shown shouldn't convince anyone to use Hashtable instead of Dictionary, since both code pieces can be used for both types.
您展示的代码不应说服任何人使用 Hashtable 而不是 Dictionary,因为这两个代码段都可以用于这两种类型。
For hashtable:
对于哈希表:
foreach(object key in h.keys)
{
string keyAsString = key.ToString(); // btw, this is unnecessary
string valAsString = h[key].ToString();
System.Diagnostics.Debug.WriteLine(keyAsString + " " + valAsString);
}
For dictionary:
对于字典:
foreach(string key in d.keys)
{
string valAsString = d[key].ToString();
System.Diagnostics.Debug.WriteLine(key + " " + valAsString);
}
And just the same for the other one with KeyValuePair, just use the non-generic version for Hashtable, and the generic version for Dictionary.
与 KeyValuePair 的另一个相同,只需对 Hashtable 使用非通用版本,对 Dictionary 使用通用版本。
So it's just as easy both ways, but Hashtable uses Object for both key and value, which means you will box all value types, and you don't have type safety, and Dictionary uses generic types and is thus better.
所以这两种方式都一样简单,但是 Hashtable 对键和值都使用 Object,这意味着您将装箱所有值类型,并且您没有类型安全,而 Dictionary 使用泛型类型,因此更好。
回答by kokos
Use something like this:
使用这样的东西:
class Tree < T > : Dictionary < T, IList< Tree < T > > >
{
}
It's ugly, but I think it will give you what you want. Too bad KeyValuePair is sealed.
这很丑陋,但我认为它会给你你想要的。太糟糕了 KeyValuePair 是密封的。
回答by Rob Cooper
Just one thing to add to this (although I do think you have already had your question answered by others). In the interests of extensibility (since we all know it will happen at some point) you may want to check out the Composite PatternThis is ideal for working with "Tree-Like Structures"..
只需添加一件事(尽管我确实认为您的问题已经得到其他人的回答)。为了可扩展性(因为我们都知道它会在某个时候发生),您可能需要查看复合模式这是使用“树状结构”的理想选择。
Like I said, I know you are only expecting one sub-level, but this could really be useful for you if you later need to extend ^_^
就像我说的,我知道你只期待一个子级别,但是如果你以后需要扩展,这对你真的很有用 ^_^
回答by Shaun Austin
I think what you might be after (as a literal implementation of your question), is:
我认为您可能会追求(作为您问题的字面实现),是:
public class TokenTree
{
public TokenTree()
{
tree = new Dictionary<string, IDictionary<string,string>>();
}
IDictionary<string, IDictionary<string, string>> tree;
}
You did actually say a "list" of key-values in your question, so you might want to swap the inner IDictionary
with a:
你确实在你的问题中说了一个键值的“列表”,所以你可能想IDictionary
用一个交换内部:
IList<KeyValuePair<string, string>>