Hashtable - Order of storage

N

nithya4u

Hi All,
Currently, am working on converting some part of C# code to JAVA. Am
having a problem with respect to the usage of Hashtable in C#. Is there
a java equivalent for it? I have searched lots and am not able to get a
solution.

In the C# code that am working on, there is an arraylist and they are
copying the values to a hashtable for easy retrieval purpose. After
making a copy, ironically, the order of values in both the arraylist
and the hastable are different.

I do not know based on which criterion this order is arrived at for
storage in hashtable. Does someone know what is criteria for this
sort...?? I have to follow the same for my Java conversion.
 
J

Jon Skeet [C# MVP]

Currently, am working on converting some part of C# code to JAVA. Am
having a problem with respect to the usage of Hashtable in C#. Is there
a java equivalent for it? I have searched lots and am not able to get a
solution.

Yes, Java has Hashtable and HashMap. (Every method of Hashtable is
synchronized; this is rarely necessary - use HashMap usually.)
In the C# code that am working on, there is an arraylist and they are
copying the values to a hashtable for easy retrieval purpose. After
making a copy, ironically, the order of values in both the arraylist
and the hastable are different.

Doing the copy won't change the order in the ArrayList. The order in
the hashtable is not specified, and you shouldn't rely on it.
I do not know based on which criterion this order is arrived at for
storage in hashtable. Does someone know what is criteria for this
sort...?? I have to follow the same for my Java conversion.

You can't. The order will change, and will depend on the hash codes
involved. Your code mustn't rely on the order in the hashtable.
 
N

nithya4u

The problem is the order of the values in the hastable is more
important for the flow. Our C# code relies on the order.

Can we get to know before hand the hash codes that will be generated
for key values of the hashtable?
 
J

Jon Skeet [C# MVP]

The problem is the order of the values in the hastable is more
important for the flow. Our C# code relies on the order.

Then your C# code is fundamentally broken.
Can we get to know before hand the hash codes that will be generated
for key values of the hashtable?

Yes, but you need to know more of the details of the implementation
than that. The order depends on the hashcode, but it won't just be
hashcode order, IIRC.

Rather than try to mimic your broken C# behaviour, you'd be *much*
better off fixing your C# code to not rely on something it shouldn't.

Jon
 
G

Greg Young [MVP]

I suggest making a "HashList" class ..

I use these quite often ... basically they are a list with a hash table for
indexing. To implement one you can just sub class ArrayList (List<> for 2.0)
and override add/remove etc adding calls to maintain your hashtable .. then
add methods to hit the index (i.e. GetByKey(key))

Cheers,

Greg
 
H

Helge Jensen

Greg said:
I suggest making a "HashList" class ..

I use these quite often ... basically they are a list with a hash table for
indexing. To implement one you can just sub class ArrayList (List<> for 2.0)
and override add/remove etc adding calls to maintain your hashtable .. then
add methods to hit the index (i.e. GetByKey(key))

That would work fine for monotonic data-structures, and is a very simple
solution, which is *good*.

Alas, there is a snake in paradise. Removing items from an ArrayList is
expensive, O(n), using a linked-list as the list backend can remove
this, if you store the linked-list reference data in a proxy for the
value. and de-proxy the value every time a value is returned.

Keeping items in a linked-list as well as a hash allows, for example,
java.util.LinkedHashSet to be O(1) on all operations, *and* provide
traversal in the order of insertion.

If you get a bit more sneaky, embedding linked-lists in the values, you
can do MRU/LRU ordering on the elements, age markers, and many other
tings that are very usefull for caches.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top