473,508 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2858
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
2812
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
15562
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
59501
by: SenthilVel | last post by:
how to get the corresponding values for a given Key in hashtable ??
33
3286
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
696
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
9680
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
1677
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
2522
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
3137
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
3864
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
7342
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
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7505
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
5650
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,...
1
5060
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...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.