.NET 中是否有一个集合可以同时充当字典和列表?

发布于 2024-09-18 23:01:45 字数 107 浏览 5 评论 0原文

我想要的基本上是一个字典和列表混合的集合。我想要一个可以添加键/值对的集合(如字典),但同时能够按照我添加它们的顺序检索值(没有键)(如列表)? .NET 中是否存在这样的集合?

谢谢

What I want is basically a collection that's a hybrid of a dictionary and a list. I want a collection that I can add key/value pairs to (like a Dictionary), but at the same be able to retrieve the values (without the keys) in the same order I added them (like a List)? Does such a collection exists in .NET?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一指流沙 2024-09-25 23:01:45

有一种非通用数据结构,称为 OrderedDictionary 可以满足您的需求。它有两个索引器,一个采用 Object 并执行键/值查找,另一个采用 int 并执行索引查找。您还可以按照添加内容的顺序枚举内容。

我在文档中没有看到任何关于字典查找是否实现O(1)(即快速)行为的内容。鉴于它实现了ISerialized,它很可能使用对象的哈希码,因此具有O(1)字典查找。

您还可以创建自己的泛型类型,其中封装了 ListDictionary

There is a non-generic data-structure called OrderedDictionary which does what you want. It has two indexers, one which takes an Object and does the key/value lookup, and one which takes an int and does an index lookup. You can also enumerate the contents in the order in which you added them.

I don't see anything in the documentation though about whether the dictionary lookup achieves O(1) (i.e. fast) behaviour. Given that it implements ISerializable it's highly likely that it uses objects' hash codes, and hence has O(1) dictionary lookup.

You could also create your own generic type which encapsulates both a List<T> and a Dictionary<TKey,TValue>.

缪败 2024-09-25 23:01:45

System.Collections.ObjectModel.KeyedCollection 与您非常接近重新要求,但字典键必须可从值派生。

System.Collections.ObjectModel.KeyedCollection is very close to what you're asking for, except the dictionary key must be derivable from the value.

我要还你自由 2024-09-25 23:01:45

不,目前框架中没有任何内容可以实现此功能(但请参阅下面的编辑)。

基本上,您希望在自己的类中编写一个 List 和一个 Dictionary

编辑:我真的忘记了 wesleyhill 指出的 OrderedDictionary - 但我不相信在这样的框架中有一个通用集合。我想您实际上想要一个通用集合?当然,您可以围绕 OrderedDictionary 编写一个包装器...

编辑:快速说明:虽然包装集合一点也不难,但您将失去通用集合的一个好处:避免装箱。当然,如果您的键和值是引用类型,则这不是问题。

No, there's nothing in the framework which implements this functionality at the moment (but see edit below).

Basically you would want to compose a List<T> and a Dictionary<TKey, TValue> in your own class.

EDIT: I had genuinely forgotten about OrderedDictionary as pointed out by wesleyhill - but I don't believe there's a generic collection in the framework like this. I assume you'd actually want a generic collection? You could write a wrapper around OrderedDictionary of course...

EDIT: Quick note: while wrapping the collection shouldn't be hard at all, you'll lose one benefit of generic collections: avoiding boxing. Not an issue if your keys and values are reference types, of course.

梦境 2024-09-25 23:01:45
  • 创建一个新类,在添加数据时包装字典
  • ,将数据包装在一个小帮助器类中,该类
  • 在返回值时引入索引,根据索引对它们进行排序

不会是性能怪物,但应该可以正常工作。否则,您可以实现自己的 LinkedHashMap 就像在 Java 中一样。

顺便说一下,看看这个:
.NET 中的 LinkedHashMap

编辑:
我更喜欢 wesleyhills 的想法:将 List 和 Dictionary 封装在一个类中。始终添加到两者并返回 List 而不是 Dictionary.Values。

  • Create a new class which wraps dictionary
  • when adding data, wrap the data in a little helper class which introduces an index
  • when returning the values, order them according the index

wouldnt be a performance monster but should work fine. Otherwise you could implement your own LinkedHashMap as in Java.

By the way, have a look at this:
LinkedHashMap in .NET

edit:
I do like wesleyhills idea even more: encapsulate List and Dictionary in one class. Add always to both and return the List instead of Dictionary.Values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文