如何使 .NET 哈希表像 Java 哈希表一样工作

发布于 2024-10-10 22:42:26 字数 1236 浏览 9 评论 0原文

当我将一些项目添加到 Java Hashtable 中时,它们的顺序与 .NET Hashtable 的顺序不同。有什么方法可以确保 .NET Hashtable 与 Java Hashtable 具有相同的顺序吗?


我正在尝试将一些 Java 代码移植到 C# 中。 Java 代码使用Hashtable 来跟踪一些数据。当我检查顺序时,当我迭代 Java Hashtable 或 .NET Hashtable (通过 >枚举器),每个都始终具有相同的数据,并且顺序相同......但是每个基于代码都有不同的顺序。

有什么方法可以使 .NET Hashtable 数据的顺序与 Java Hashtable 相同吗?

我知道 Hashtable 不处理排序 - 所以我觉得没有什么可以做的。我也无法将 Java 代码中的数据类型从 .. 比如说 .. Hashtable 更改为其他类型。

这是一些相同的数据来说明我的情况。

数据,为任一代码库按顺序添加:-

  1. num |一些数据
  2. 页数 |一些数据
  3. x |一些数据
  4. 顶部 | someData

Java 代码:

private Hashtable identifiers = new Hashtable();
...
identifiers.put(symbol, identifier);

通过枚举器迭代的 Java 输出:

alt text

.NET 代码:

private Hashtable Identifiers = new Hashtable();
...
Identifiers.Add(symbol, identifier);

通过枚举器迭代的 .NET 输出。

alt text

有什么想法或建议吗?

When I add some items into a Java Hashtable, their order is different to that of a .NET Hashtable. Is there any way I can make sure the .NET Hashtable will have the same order as a Java Hashtable?


I'm trying to port some Java code to C#. The Java code uses a Hashtable to keep track of some data. When I check to see the order the data is retrieved when I iterate through either the Java Hashtable or the .NET Hashtable (via an Enumerator), each one consistently has the same data, in the same order ... but each code based has a different order.

Is there any way I can make it so that the .NET Hashtable data is in the same order as the Java Hashtable?

I understand that Hashtable do not handling ordering - so I feel like there's nothing that can be done. I also cannot change the datatype in the Java code from .. say .. a Hashtable to something else.

Here's some same data to illustrate my situation.

Data, added sequential for either code base :-

  1. num | someData
  2. pagenum | someData
  3. x | someData
  4. top | someData

Java Code:

private Hashtable identifiers = new Hashtable();
...
identifiers.put(symbol, identifier);

Java output via iteration over an enumerator:

alt text

.NET code:

private Hashtable Identifiers = new Hashtable();
...
Identifiers.Add(symbol, identifier);

.NET output via iteration over an enumerator.

alt text

Any ideas or suggestions?

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

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

发布评论

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

评论(7

少钕鈤記 2024-10-17 22:42:26

不幸的是,如果不改变集合的类型,实际上没有办法解决这个问题。哈希表不保证顺序(正如您在帖子中提到的)。

这一切都可以归结为以下几点:

如果顺序很重要,那么哈希表就是错误的数据结构。

Unfortunately, short of changing the type of the collection there's really no way around this. Hashtables don't guarantee order (as you mentioned in your post).

It all boils down to the following:

If order matters, a Hashtable is the wrong data structure.

追风人 2024-10-17 22:42:26

如果不提取键并以某种方式对它们进行排序(或使用有序字典),则无法更改此顺序。您正在使用的哈希表实现的迭代顺序均未指定,因此依赖特定顺序意味着您的代码已损坏。Java 中的迭代顺序甚至不能保证在JDK 的未来版本。

You cannot change this order short of extracting the keys and sorting them in some way (or using an ordered dictionary). The iteration orders of either of the hashtable implementations you are using are not specified, and therefore relying on a specific order means your code is broken. The iteration order in Java is not even guaranteed to remain the same in future versions of the JDK.

ゃ懵逼小萝莉 2024-10-17 22:42:26

通用哈希表中的顺序是不可预测的。这就是你正在经历的。如果您尝试运行比较测试并且需要特定顺序,则必须在两端使用有序映射。否则几乎没有办法预测输出。

The order in the generic hashtable is unpredictable. That's what you are experiencing. If you are trying to run comparison tests and if you need specific order, you have to use ordered maps on BOTH ends. Otherwise there isn't hardly a way to predict the output.

请别遗忘我 2024-10-17 22:42:26

我同意其他答案,但 Hashtable 仍然已被弃用。您应该始终使用通用的 Dictionary 类。还有一个SortedDictionary,类名说明了一切。您可以对其内容进行反转或排序,请查看此处 .NET 中的反向排序字典

I agree with other answers, but still, Hashtable is deprecated. You should always use a generic Dictionary class. There is also a SortedDictionary, class name says all. you can reverse or sort it's content, take a look here Reverse Sorted Dictionary in .NET

執念 2024-10-17 22:42:26

您可以在 Java 中使用 LinkedHashMap,递归软件根据这篇文章。

我还没有尝试过这个解决方案。

You could use a LinkedHashMap in Java and Recursion Software makes one available for C# as per this post.

I have not tried this solution.

俯瞰星空 2024-10-17 22:42:26

这是一个糟糕的解决方案,但根据您的描述,您别无选择。

您可以获取 Java Hastable 类的源代码并将它们移植到 C#。去掉所有不必要的东西,比如迭代器和 keySet、equals,你将拥有相当小的类(我期望只有几十行)。将其转换为 C# 应该很简单。

当然,除了 Java 兼容性之外,您不应该将这个新类用于任何其他目的。向原始应用程序供应商提交错误也可能很好:)

It's a terrible solution, but according to your description you have no other option.

You can take sources of Java Hastable class and port them to C#. Strip down everything unnecessary, like iterators and keySet, equals, and you will have pretty small class (few dozen lines I'd expect). Converting it to C# should be trivial.

Naturally, you shouldn't use this new class for anything other than Java compatibility. And filing a bug with original application vendor might be nice too :)

简美 2024-10-17 22:42:26

您可以尝试(在.NET 中)实现您自己的 IHashCodeProvider 和 IComparer 并将它们传递给 Hashtable 构造函数。
从您发布的示例中可以看出,条目是按键升序(.NET)和降序(Java)排序的,因此您需要一个反转顺序的比较器。

You could try to implement (in .NET) your own IHashCodeProvider and IComparer and pass those to the Hashtable constructor.
From the example you posted it appears that the entries are sorted on the key ascending (.NET) and descending (Java) order so you'd need a comparer that inverts the order.

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