473,394 Members | 1,956 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,394 software developers and data experts.

Hashtable

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 order?

Jun 14 '07 #1
5 2844
rk****@gmail.com wrote:
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 order?
Not using a HashTable. You would have to also add them to a collection
that does retain the order.

--
Göran Andersson
_____
http://www.guffa.com
Jun 14 '07 #2
On Jun 14, 5:39 pm, Göran Andersson <g...@guffa.comwrote:
rk2...@gmail.com wrote:
I am usingHashtableto keep Key-Value pair of elements.
When I add the items to theHashtableit does not retain the order in
which I have added the key-value pair.
Is there anyway to retain the order?

Not using aHashTable. You would have to also add them to a collection
that does retain the order.

--
Göran Andersson
_____http://www.guffa.com

Is there a collection that retains the order?

Jun 14 '07 #3
Howdy,

Hastable does not retain the order because items are oragnised by the key
hash code, allowing access in constant number of operations O(n). You would
have to create an additional list containing keys:

Hashtable hashtable =
new Hashtable();

List<stringhistory =
new List<string>();

for (int i = 0; i < 10; i++)
{
string key = Guid.NewGuid().ToString();

hashtable.Add(key, Guid.NewGuid());
history.Add(key);
}

// show how the items are organised in hashtable
foreach (DictionaryEntry pair in hashtable)
{
System.Diagnostics.Debug.WriteLine(
pair.Key.ToString() + "=" +
pair.Value.ToString());
}

System.Diagnostics.Debug.WriteLine(String.Empty);

// show items in order they were added
foreach (string key in history)
{
System.Diagnostics.Debug.WriteLine(
key + "=" +
hashtable[key].ToString());
}

Hope this helps

Milosz
--
Milosz
"rk****@gmail.com" wrote:
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 order?

Jun 14 '07 #4
rk****@gmail.com wrote:
On Jun 14, 5:39 pm, Göran Andersson <g...@guffa.comwrote:
>rk2...@gmail.com wrote:
>>I am usingHashtableto keep Key-Value pair of elements.
When I add the items to theHashtableit does not retain the order in
which I have added the key-value pair.
Is there anyway to retain the order?
Not using aHashTable. You would have to also add them to a collection
that does retain the order.

--
Göran Andersson
_____http://www.guffa.com


Is there a collection that retains the order?
Most of them. I can only think of HashTable, Dictionary<and
SortedList<that doesn't.

--
Göran Andersson
_____
http://www.guffa.com
Jun 14 '07 #5
The "Collection" object in the VB namespace does what you want, and there is
no equivalent currently built into C# or the .NET Framework.
But you can reference it and use it even if you're using C#.

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net
<rk****@gmail.comwrote in message
news:11**********************@j4g2000prf.googlegro ups.com...
>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 order?
Jun 15 '07 #6

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

Similar topics

5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
5
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
8
by: SenthilVel | last post by:
how to get the corresponding values for a given Key in hashtable ??
33
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
16
by: Sreekanth | last post by:
Hello, Is there any better collection than HashTable in terms of performance, when the type of the key is integer? Regards, Sreekanth.
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
8
by: Robin Tucker | last post by:
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed...
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...
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
2
by: archana | last post by:
Hi all, I am having one confusion regarding hashtable. I am having function in which i am passing hashtable as reference. In function i am creating one hashtable which is local to that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.