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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With