473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Hashtable - Order of storage

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.

Apr 28 '06 #1
9 7236
<ni******@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 28 '06 #2
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?

Apr 28 '06 #3
ni******@gmail.com wrote:
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

Apr 28 '06 #4
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
<ni******@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
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?

Apr 28 '06 #5
ni******@gmail.com wrote:
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

Apr 28 '06 #6
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
<ni******@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
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?

Apr 28 '06 #7


Greg Young [MVP] wrote:
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.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Apr 28 '06 #8
If you care about the order use SortedList or SortedDictionary class.

--
<ni******@gmail.com> schrieb im Newsbeitrag
news:11**********************@e56g2000cwe.googlegr oups.com...
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.

Apr 28 '06 #9


cody wrote:
If you care about the order use SortedList or SortedDictionary class.


Those doesn't sort by order of storage, but by key.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Apr 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
I'm using an enumerator to iterate through a HashTable that contains all numeric keys. I'd like to iterarate through the HashTable based on the ordered keys. Is there a quick way to do this?...
0
by: Ricardo Pereira | last post by:
Hi, I wrote a simple program to test the "out-of-the-box" functionalities of the CMAB (Configuration Management Application Block). After having added 2 values to an hashtable, and having written...
0
by: choyk1 | last post by:
I intended to save properties of an object to a Hashtable. In this case, keys and values are not fixed types and I cannot use SortedList. I have no idea what order the .NET framework add items to...
2
by: Ali | last post by:
I am binding a hashTable to a dropDownList to pick a State (key: like New York) and sends the state designation (value: NY) to a filtering procedure. I have entered the states in the hashTable in...
7
by: SevDer | last post by:
Hi We have a static hashtable that is located in another tier in our n-tiered web application. And we are storing big but not huge objects in this hashtable and the key to the objects is...
3
by: Lamis | last post by:
Hi, I have a hashtabel m_haschtbl = new Hashtable(); I need to get out my objects from my hashtable in the same order like they were stored. the order is very important.. I have used ...
10
by: chrisben | last post by:
Hi, Here is the scenario. I have a list of IDs and there are multiple threads trying to add/remove/read from this list. I can do in C# 1. create Hashtable hList = Hashtable.Synchronized(new...
5
by: rk2008 | last post by:
I am using Hashtable to keep Key-Value pair of elements. When I add the items to the Hashtable it does not retain the order in which I have added the key-value pair. Is there anyway to retain the...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.