.NET 中的 LinkedHashMap
我想知道.NET 中是否有 java.util.LinkedHashMap 的对应项? (即,如果我访问元素,元素会自动(重新)排序。 (boolean accessOrder) )。
I wonder if there is a counterpart to java.util.LinkedHashMap
in .NET? (ie. the elements are (re)ordered automatically if I access an element. (boolean accessOrder) ).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
只是为了向读者澄清一点:LinkedHashMap 仅在使用一个特定的构造函数重载构建时才会出现这种行为。 通常,元素按插入顺序维护。 (这对我来说有点奇怪,但没关系。)
我不相信 .NET 中有任何这样的类。 使用元素的链表和从键到链表节点的字典来构建一个并不会太难。 然后,访问将包括获取链表节点、将其移至头部并返回值。
如果您愿意的话,我很乐意今晚或明天实现它 - 尽管可能不会进行完整的单元测试等。(完全测试集合是一项耗时的工作!)
Just to clarify a bit for readers: LinkedHashMap only behaves that way when built with one particular constructor overload. Normally the elements are maintained in insert order. (This feels a little odd to me, but never mind.)
I don't believe there's any such class in .NET. It wouldn't be too hard to build one, using a linked list of elements and a dictionary from key to linked list node. Access would then consist of fetching the linked list node, moving it to the head, and returning the value.
I'd be happy to implement it tonight or tomorrow if you want - although probably not with full unit tests etc. (Fully testing a collection is a time-consuming business!)
谷歌搜索似乎表明,LinkedHashMap 没有内置的 C# 等效项,但有一些第三方选项可用。
A bit of Googling seems to show that there is no built in C# equivalent for LinkedHashMap, but there are some third party options available.
这是我在 论坛 上找到的 C# 实现:
它没有文档记录,但是确实有一些测试。 然而,它并不通用。 至少我猜是这样。
@Jon:如果您能快速实施,我也将不胜感激。 我认为 LinkedList 之上的字典是最好的,但是 我听说< /a> LinkedList 存在垃圾收集问题,会减慢速度。
Here's a C# implementation I found on a forum:
It's undocumented, but does have some tests. It is not generic, however. At least it's something I guess.
@Jon: I'd appreciate it too if you could do a quick implementation. I imagined that a Dictionary on top of a LinkedList would be best, but I hear there are garbage collection issues with LinkedList that slows things down.
我使用 System.Collections.Specialized.OrderedDictionary 作为 LinkedHashMap 的替代品。 这对我有用。 关于 OrderedDictionary 我有什么遗漏的吗(是的,它不是通用的,但它可以在 .Net 2 或更高版本中使用)?
I used System.Collections.Specialized.OrderedDictionary as a replacement for LinkedHashMap. It worked for me. Is there anything I'm missing about OrderedDictionary (yes, it's not generic, but it is available with .Net 2 or newer)?
我知道这是一个老话题,但是有一个很棒的开源项目可以在 .NET 中实现
LinkedHashMap
C5这是LinkedHashMap< /a> 源代码。
I know this is an old topic, but there is an awesome open source project to implement
LinkedHashMap
in .NET C5Here is LinkedHashMap source code.
Nhibernate 有一个 NHibernate.Util.LinkedHashMap 实现。
如果你的代码中已经有它了,就像我一样,它会很方便
Nhibernate has a NHibernate.Util.LinkedHashMap implementation.
If you already have it on your code, as I had, it can be handy
由于 C# 中仍然没有 LinkedHashMap,而我需要此功能,因此我在最新的 net core (3.1) 上实现了一个。 https://github.com/idlerboris/LinkedHashMap/blob/ master/CustomCollections/CustomCollections/LinkedHashMap.cs。 它涵盖了基本测试,看起来不错,但请随意贡献/报告问题。
As there is still no LinkedHashMap in C#, and I needed this functionality, I've implemented one on the latest net core (3.1). https://github.com/idlerboris/LinkedHashMap/blob/master/CustomCollections/CustomCollections/LinkedHashMap.cs. It's covered with basic tests and seems to be good, but feel free to contribute/report issues.
进入这个游戏已经很晚了,但我在 C# 中实现了 LinkedHashMap (Java) 等效项 LinkedDictionary,如下所示:
以及一个简单的 UnitTest,我将它与 Dictionary 进行比较
因为它基于 Dictionary 和 List,所以它至少增加了时间复杂度列表访问和字典访问。
pretty late to the game, but I implemented the LinkedHashMap (Java) equivalent in C# as LinkedDictionary as follows:
And a simple UnitTest where I compare it to Dictionary
As it is based on a Dictionary and a List it at least adds the time complexity of List accesses and Dictionary accesses.