[英]How to make a .NET Hashtable work just like a Java Hashtable
When I add some items into a Java Hashtable
, their order is different to that of a .NET Hashtable
. 当我将某些项目添加到Java
Hashtable
,它们的顺序与.NET Hashtable
的顺序不同。 Is there any way I can make sure the .NET Hashtable
will have the same order as a Java Hashtable
? 有什么方法可以确保.NET
Hashtable
与Java Hashtable
具有相同的顺序?
I'm trying to port some Java code to C#. 我正在尝试将一些Java代码移植到C#。 The Java code uses a
Hashtable
to keep track of some data. Java代码使用
Hashtable
来跟踪某些数据。 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. 当我查看通过Java
Hashtable
或.NET Hashtable
(通过Enumerator
)进行迭代时,检索数据的顺序时,每个数据始终以相同的顺序具有相同的数据...但是每个基于代码的都有不同的顺序。
Is there any way I can make it so that the .NET Hashtable
data is in the same order as the Java Hashtable
? 有什么办法可以使.NET
Hashtable
数据与Java Hashtable
处于相同顺序?
I understand that Hashtable
do not handling ordering - so I feel like there's nothing that can be done. 我了解
Hashtable
不处理排序-因此我觉得无能为力。 I also cannot change the datatype in the Java code from .. say .. a Hashtable
to something else. 我也无法将Java代码中的数据类型从..例如..
Hashtable
更改为其他内容。
Here's some same data to illustrate my situation. 这是一些相同的数据来说明我的情况。
private Hashtable identifiers = new Hashtable();
...
identifiers.put(symbol, identifier);
private Hashtable Identifiers = new Hashtable();
...
Identifiers.Add(symbol, identifier);
Any ideas or suggestions? 有什么想法或建议吗?
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. 如果顺序很重要,则哈希表是错误的数据结构。
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.
Java的迭代顺序甚至不能保证在JDK的未来版本中保持不变。
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.
否则,几乎没有办法预测输出。
I agree with other answers, but still, Hashtable is deprecated. 我同意其他答案,但是仍然不推荐使用Hashtable。 You should always use a generic Dictionary class.
您应该始终使用通用的Dictionary类。 There is also a SortedDictionary, class name says all.
还有一个SortedDictionary,类名全部说明。 you can reverse or sort it's content, take a look here Reverse Sorted Dictionary in .NET
您可以对它的内容进行反向或排序,请看这里.NET中的反向排序字典
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#. 您可以获取Java
Hastable
类的源并将其移植到C#。 Strip down everything unnecessary, like iterators and keySet
, equals
, and you will have pretty small class (few dozen lines I'd expect). 剥离所有不必要的东西,例如iterators和
keySet
, equals
,您将拥有非常小的类(我希望能打几行)。 Converting it to C# should be trivial. 将其转换为C#应该很简单。
Naturally, you shouldn't use this new class for anything other than Java compatibility. 自然,除了Java兼容性之外,您不应将此新类用于任何其他用途。 And filing a bug with original application vendor might be nice too :)
向原始应用程序供应商提交错误可能也不错:)
You could try to implement (in .NET) your own IHashCodeProvider and IComparer and pass those to the Hashtable constructor. 您可以尝试在.NET中实现自己的IHashCodeProvider和IComparer并将它们传递给Hashtable构造函数。 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.
从您发布的示例中可以看出,条目是按键的升序(.NET)和降序(Java)顺序排序的,因此您需要一个将顺序反转的比较器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.