java .NET 中的 LinkedHashMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/486948/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:36:42  来源:igfitidea点击:

LinkedHashMap in .NET

java.net.net-3.5linkedhashmap

提问by Schildmeijer

I wonder if there is a counterpart to java.util.LinkedHashMapin .NET? (ie. the elements are (re)ordered automatically if I access an element. (boolean accessOrder) ).

我想知道java.util.LinkedHashMap在.NET中是否有对应的?(即,如果我访问一个元素,元素会自动(重新)排序。(布尔访问顺序))。

采纳答案by Adam Ralph

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.

一些谷歌搜索似乎表明 LinkedHashMap 没有内置的 C# 等效项,但有一些第三方选项可用。

回答by Jon Skeet

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.)

只是为了向读者澄清一点:LinkedHashMap 仅在使用一个特定的构造函数重载构建时才会以这种方式运行。通常,元素按插入顺序维护。(这对我来说有点奇怪,但没关系。)

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.

我不相信 .NET 中有任何这样的类。使用元素的链表和从键到链表节点的字典来构建一个并不太难。然后访问将包括获取链表节点,将其移动到头部,然后返回值。

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!)

如果您愿意,我很乐意在今晚或明天实施它 - 尽管可能没有完整的单元测试等(完全测试集合是一项耗时的工作!)

回答by biozinc

Here's a C# implementation I found on a forum:

这是我在论坛上找到的 C# 实现:

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 hearthere are garbage collection issues with LinkedList that slows things down.

@Jon:如果您能快速实现,我也将不胜感激。我认为在 LinkedList 之上的 Dictionary 会是最好的,但我听说LinkedList 存在垃圾收集问题,这会减慢速度。

回答by Nico

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)?

我使用 System.Collections.Specialized.OrderedDictionary 作为 LinkedHashMap 的替代品。它对我有用。我对 OrderedDictionary 有什么遗漏(是的,它不是通用的,但它可用于 .Net 2 或更新版本)?

回答by Luiz Henrique Martins Lins Rol

Nhibernate has a NHibernate.Util.LinkedHashMap implementation.

Nhibernate 有一个 NHibernate.Util.LinkedHashMap 实现。

If you already have it on your code, as I had, it can be handy

如果你的代码中已经有了它,就像我一样,它会很方便

回答by Boris Parfenenkov

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,而我需要这个功能,我已经在最新的网络核心 (3.1) 上实现了一个。https://github.com/idlerboris/LinkedHashMap/blob/master/CustomCollections/CustomCollections/LinkedHashMap.cs。它涵盖了基本测试,看起来不错,但可以随意贡献/报告问题。