473,396 Members | 2,147 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,396 software developers and data experts.

Iterating through a HashTable

I have a hashtable keyed by some name, holding an instance of an object. Most
of the time I use the hashtable in the traditional sense, given a name, I
lookup the object, then I run a method on that object. Occaisionally however
I need to run a method on every object in the hashtable. I've done both
For..Each and iEnumeratior loops. The messy part is that under certain
conditions, while iterating, a condition exists where I need to remove an
object from the hashtable. With ArrayLists I handle this with a reverse
iteration (from last to first) and do a .RemoveAt. Hashtables do not have
..RemoveAt. For..Each doesnt work because .Remove needs the key and I only
have the object, iEnumerator doesn't work because I get an error after the
first .Remove saying my enumerator is now invalid, so I can't continue that
iteration.

I've thought about several solutions mostly inlvolving separate threads, but
wanted to know if there was a cleaner way to handle this. Anyone come across
this? It seems like a fairly standard problem, there must be some "pattern"
solution.
-Ken
Nov 21 '05 #1
10 15059
Try making a copy of the keys collection if it isn't too expensive.

Dim h As New Hashtable
Dim key, value As Object
Dim keys As ICollection = h.Keys
For Each key In keys
value = h(key)
If value.condition Then
h.Remove(key)
End If
Next

HTH
Nov 21 '05 #2
Uuhm... maybe actually making a copy would help.

Dim key, value As Object
Dim keys As ICollection = h.Keys
Dim keylist As New ArrayList
keylist.AddRange(keys)
For Each key In keylist
If CInt(key) > 5 Then
h.Remove(key)
End If
Next

"Jonas Pohlandt" <j.******************@dbit-systems.de> schrieb im
Newsbeitrag news:OE**************@tk2msftngp13.phx.gbl...
Try making a copy of the keys collection if it isn't too expensive.

Dim h As New Hashtable
Dim key, value As Object
Dim keys As ICollection = h.Keys
For Each key In keys
value = h(key)
If value.condition Then
h.Remove(key)
End If
Next

HTH

Nov 21 '05 #3
Ken,

I have the idea that you are not iterating through the keys of the
hasthtable but throught the values.

In my idea is that not where the hashtable is not made for, in that case by
instance the datatable does a much better job as collection because you have
much more methods to get the values from the objects in the datarow and
remove when needed those rows.

However maybe I see something wrong?

Cor
Nov 21 '05 #4
I've considered the copy solution, but given that I don't know in advance my
max item count, there'd be this potential baggage hanging over my head of a
duplicate collection. Also, because I'm using the 'value' side of the
hashtable, then based on some state need to delete it, I can't quite get the
logic in place even using a copy.

My solution so far is to do my iteration with a SyncLock wrapper and spawn a
separate thread for each 'Remove', then .NET will handle the blocking for me,
and the removes will occur after the iteration pass is complete. Or build an
array list of keys to delete as i'm iterating, and do a second for loop pass
on that array list. I was just hoping there was a way to get hash tables to
work the way i want inherintly.

"Jonas Pohlandt" wrote:
Uuhm... maybe actually making a copy would help.

Dim key, value As Object
Dim keys As ICollection = h.Keys
Dim keylist As New ArrayList
keylist.AddRange(keys)
For Each key In keylist
If CInt(key) > 5 Then
h.Remove(key)
End If
Next

"Jonas Pohlandt" <j.******************@dbit-systems.de> schrieb im
Newsbeitrag news:OE**************@tk2msftngp13.phx.gbl...
Try making a copy of the keys collection if it isn't too expensive.

Dim h As New Hashtable
Dim key, value As Object
Dim keys As ICollection = h.Keys
For Each key In keys
value = h(key)
If value.condition Then
h.Remove(key)
End If
Next

HTH


Nov 21 '05 #5
Well, I simplified my model for brevity. What I have is more complicated but
boils down to a hash table of open TCP sockets with the key being some
internal identifier. Normal processing is, given an identifier, I do
mySocket = myHashTable(identifier)
mySocket.send("blah")
again, this is simplified for example sake. it's not really like this there
is a lot of async calls and callbacks etc. Now, if I check and mySocket is
not connected, or if i get an error back from .send, I shutdown that socket
and remove it from the hashtable. When processing one at a time, this works
fine.

My problem is that there is one instance where I have to loop through the
entire hash table of sockets and do a .Send. In that instance, if i have a
bad socket and want to remove it from the Hashtable...i get the problem i
originally described. So, databases, etc. don't fit my problem domain.

-Ken

"Cor Ligthert" wrote:
Ken,

I have the idea that you are not iterating through the keys of the
hasthtable but throught the values.

In my idea is that not where the hashtable is not made for, in that case by
instance the datatable does a much better job as collection because you have
much more methods to get the values from the objects in the datarow and
remove when needed those rows.

However maybe I see something wrong?

Cor

Nov 21 '05 #6
Well, I simplified my model for brevity. What I have is more complicated but
boils down to a hash table of open TCP sockets with the key being some
internal identifier. Normal processing is, given an identifier, I do
mySocket = myHashTable(identifier)
mySocket.send("blah")
again, this is simplified for example sake. it's not really like this there
is a lot of async calls and callbacks etc. Now, if I check and mySocket is
not connected, or if i get an error back from .send, I shutdown that socket
and remove it from the hashtable. When processing one at a time, this works
fine.

My problem is that there is one instance where I have to loop through the
entire hash table of sockets and do a .Send. In that instance, if i have a
bad socket and want to remove it from the Hashtable...i get the problem i
originally described. So, databases, etc. don't fit my problem domain.

-Ken
"Cor Ligthert" wrote:
Ken,

I have the idea that you are not iterating through the keys of the
hasthtable but throught the values.

In my idea is that not where the hashtable is not made for, in that case by
instance the datatable does a much better job as collection because you have
much more methods to get the values from the objects in the datarow and
remove when needed those rows.

However maybe I see something wrong?

Cor

Nov 21 '05 #7
Well, I simplified my model for brevity. What I have is more complicated but
boils down to a hash table of open TCP sockets with the key being some
internal identifier. Normal processing is, given an identifier, I do
mySocket = myHashTable(identifier)
mySocket.send("blah")
again, this is simplified for example sake. it's not really like this there
is a lot of async calls and callbacks etc. Now, if I check and mySocket is
not connected, or if i get an error back from .send, I shutdown that socket
and remove it from the hashtable. When processing one at a time, this works
fine.

My problem is that there is one instance where I have to loop through the
entire hash table of sockets and do a .Send. In that instance, if i have a
bad socket and want to remove it from the Hashtable...i get the problem i
originally described. So, databases, etc. don't fit my problem domain.

-Ken

"Cor Ligthert" wrote:
Ken,

I have the idea that you are not iterating through the keys of the
hasthtable but throught the values.

In my idea is that not where the hashtable is not made for, in that case by
instance the datatable does a much better job as collection because you have
much more methods to get the values from the objects in the datarow and
remove when needed those rows.

However maybe I see something wrong?

Cor

Nov 21 '05 #8
You can try using the 'HashList':
http://www.codeproject.com/csharp/ha...arget=hashlist

hope that helps..
Imran.

"Ken Foster" <Ke*******@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
I have a hashtable keyed by some name, holding an instance of an object. Most of the time I use the hashtable in the traditional sense, given a name, I
lookup the object, then I run a method on that object. Occaisionally however I need to run a method on every object in the hashtable. I've done both
For..Each and iEnumeratior loops. The messy part is that under certain
conditions, while iterating, a condition exists where I need to remove an
object from the hashtable. With ArrayLists I handle this with a reverse
iteration (from last to first) and do a .RemoveAt. Hashtables do not have
.RemoveAt. For..Each doesnt work because .Remove needs the key and I only
have the object, iEnumerator doesn't work because I get an error after the
first .Remove saying my enumerator is now invalid, so I can't continue that iteration.

I've thought about several solutions mostly inlvolving separate threads, but wanted to know if there was a cleaner way to handle this. Anyone come across this? It seems like a fairly standard problem, there must be some "pattern" solution.
-Ken

Nov 21 '05 #9
Interesting, and worth noteing for possible use in the future, but the
problem it solves (being able to iterate through a hashtable in FIFO order)
is not the problem I have, I don't care what order it's in. And at first
blush it looks like the .Remove during an enumeration will have the same
problems that a regular Hashtable has.

Thanks for that though.

"Imran Koradia" wrote:
You can try using the 'HashList':
http://www.codeproject.com/csharp/ha...arget=hashlist

hope that helps..
Imran.

"Ken Foster" <Ke*******@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
I have a hashtable keyed by some name, holding an instance of an object.

Most
of the time I use the hashtable in the traditional sense, given a name, I
lookup the object, then I run a method on that object. Occaisionally

however
I need to run a method on every object in the hashtable. I've done both
For..Each and iEnumeratior loops. The messy part is that under certain
conditions, while iterating, a condition exists where I need to remove an
object from the hashtable. With ArrayLists I handle this with a reverse
iteration (from last to first) and do a .RemoveAt. Hashtables do not have
.RemoveAt. For..Each doesnt work because .Remove needs the key and I only
have the object, iEnumerator doesn't work because I get an error after the
first .Remove saying my enumerator is now invalid, so I can't continue

that
iteration.

I've thought about several solutions mostly inlvolving separate threads,

but
wanted to know if there was a cleaner way to handle this. Anyone come

across
this? It seems like a fairly standard problem, there must be some

"pattern"
solution.
-Ken


Nov 21 '05 #10
Ken,
If my routine normally wanted to remove a minority of the entries, I would
consider using a For Each on the HashTable, adding each entry to remove to
an ArrayList. Then when I finished the HashTable I would use a For Each on
the ArrayList removing each item from the HashTable.

If my normally wanted to remove a majority of the entries, I would consider
using a For Each on the HashTable, adding each entry to save to a new
HashTable. Then when I finished I would replace the old hash table with the
new hashtable. Alternatively I might Clone the HashTable, clear the original
& For Each on the Clone...

I have used variations of Jonas's idea also.

Hope this helps
Jay

"Ken Foster" <Ke*******@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
I have a hashtable keyed by some name, holding an instance of an object.
Most
of the time I use the hashtable in the traditional sense, given a name, I
lookup the object, then I run a method on that object. Occaisionally
however
I need to run a method on every object in the hashtable. I've done both
For..Each and iEnumeratior loops. The messy part is that under certain
conditions, while iterating, a condition exists where I need to remove an
object from the hashtable. With ArrayLists I handle this with a reverse
iteration (from last to first) and do a .RemoveAt. Hashtables do not have
.RemoveAt. For..Each doesnt work because .Remove needs the key and I only
have the object, iEnumerator doesn't work because I get an error after the
first .Remove saying my enumerator is now invalid, so I can't continue
that
iteration.

I've thought about several solutions mostly inlvolving separate threads,
but
wanted to know if there was a cleaner way to handle this. Anyone come
across
this? It seems like a fairly standard problem, there must be some
"pattern"
solution.
-Ken

Nov 21 '05 #11

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

Similar topics

34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
1
by: bdwise | last post by:
I have a hash table with key/value like this: aaaa 1 aa 2 bbbb 3 I am iterating over a datatable and trying to search and replace values in it based on a match in the hashtable. ...
6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
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...
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...
2
by: shawnn | last post by:
Hello, Using ConfigurationSettings.AppSettings.keys I can iterate across the default AppSettings section of the XML config file. I need to iterate across all key value pairs in other sections...
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...
5
by: RSH | last post by:
I have two HashTables (_CompanyDeductions,_CompanyAccruals) that contain several objects each. I am trying to loop through and print the properties of the objects in the Hashtables: I am trying...
4
by: Dahab | last post by:
Hi, Anyone know if its possible to iterate through a hashtable in the same order that the table was populated? Im currently using an enumerator to iterate, but the order is quit different from...
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
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
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: 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:
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
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
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.