How to get a value from a hashtable without knowing the key/id?

How to get a value from a hashtable without knowing the key/id?
I have a hashtable filled with data, and I need to process the data in the hashtable one at a time.
What I want is this;


hashtable with data;
a loop till hashtable = empty
randomly get 1 value or the top value or bottom value, whatever it doesnt matter.
msgbox value
delete value from hashtable
continue loop till hasttable is empty


thx
OctalysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

doobdaveCommented:
HI,

Try something like this:

Dim htbl As Hashtable
Dim en As IDictionaryEnumerator

en = htbl.GetEnumerator

Do While Not IsNothing(en.Entry)
    MessageBox.Show("Key: " & en.Key.ToString & vbCrLf & "Value: " & en.Value)
    htbl.Remove(en.Key)

    en.MoveNext()
Loop


Regards,

David
doobdaveCommented:
Sorry, this line:
MessageBox.Show("Key: " & en.Key.ToString & vbCrLf & "Value: " & en.Value)

should be:
MessageBox.Show("Key: " & en.Key.ToString & vbCrLf & "Value: " & en.Value.ToString)
Bob LearnedCommented:
It sounds like a hash table is not the best weapon of choice :)

Bob
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

OctalysAuthor Commented:
there are too much data, it only goes well in a hashtable.
I wish there was a simpler way.
Bob LearnedCommented:
What is the benefit of a Hashtable over an ArrayList?

Bob
OctalysAuthor Commented:
[quote]
Comment from TheLearnedOne
Date: 06/27/2005 09:52PM CEST
 Comment  


Do you understand the nature of hash tables, and Big-O notation?
http://en.wikipedia.org/wiki/Big_O_notation

O(1) is constant, meaning that the process time is not dependent on structure size
O(n) means that as you add more elements the time gets linearly worse.
O(n^2) mean that the time gets quadratically worse.

Hash tables are O(1). Arraylist doesn't benefit from this.
[/quote]

hehe, the hashlist is 30K big.
Bob LearnedCommented:
Ouch!!!

Bob
Bob LearnedCommented:
You got me, pardner!

If you have to search for items sequentially, though, then you lose all the benefit of the hash table.

Bob
OctalysAuthor Commented:
yeah I know this is quite a pain in the ass, because I need to do three things with the list.

Compare.
Store alot of data.
Get one value at a time.


I dont need to search items sequentially, I just want A record from the hash table, does that make any diffrence?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
I don't think doobdaves code will work because once you remove an item from the hashtable the enumerator will be invalidated and you will get an error.

I believe you have to resort to shenanigans such as this:

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim ht As New Hashtable
        ht.Add("one", DateTime.Now)
        ht.Add("two", DateTime.Now)
        ht.Add("three", DateTime.Now)

        Dim de As DictionaryEntry
        Dim ie As IEnumerator
        While ht.Count > 0
            ie = ht.GetEnumerator ' get a new enumerator each time...
            ie.MoveNext() ' move it to the first entry
            de = CType(ie.Current, DictionaryEntry) ' grab the first entry

            ' display the key/value pair
            Debug.WriteLine("Key: " & de.Key)
            Debug.WriteLine("Value: " & de.Value)

            ' remove the current item
            ht.Remove(de.Key)
        End While
    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
@Idle_Mind
Mike, what do you think about measuring the benefit of using a Hashtable with sequential searching, versus using an ArrayList with indexed item retrieval?

Bob
Bob LearnedCommented:
BTW, using the enumerator with MoveNext is by it's very nature sequential.

Bob
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
I understand that an enumerator is sequential silly...  =)

...but the requirement was that we "delete value from hashtable" before we "continue loop till hasttable is empty".  This will invalidate the current enumerator and throw and exception when you use MoveNext() again.  

Thus the shenanigans...
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Octalys,

I think we would need more detail on how you are acessing and comparing these records of yours before we can recommend a HashTable, an ArrayList, or something completely different.

Mike
Bob LearnedCommented:
@Idle_Mind:
You are very correct about invalidating the enumerator.  

My question was about determining which would be the best approach.  

(1) Using a hash table, and then lose the benefit of O(1) with adding by sequentially looping through each item using the enumerator.

(2) Using an ArrayList, and then gain the benefit of List(Index).

Bob
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
I don't think we know enough about the process Octalys is using.

I think he is grabbing each item sequentially, and then from data in that item, probably needs to get information from another different item in the hashtable to make a decision (thus the need for searching by key).

But I'm just speculating here...

If that is the case then neither structure will work very well and the only good solution would be to use some kind of database system that would allow us to query the data and sequentially iterate it.  (unless of course we keep two copies of the data in memory, one in a hastable and one in an array, but that's not very practical)

Mike
OctalysAuthor Commented:
Well the function does this first.

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21471948.html

It merge two big lists together, then it has to go tru the list one by one to process it. Because its threaded, I remove it from the list when its processed.

Thank you for the help.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Ok...so we are working with a HashTable because that is how you went about removing the duplicates.  But do you really need to remove items from the HashTable while you are enumerating it?
OctalysAuthor Commented:
well I am working with threads to process the hasttable, i thought removing items from the hashtable is the simplest/best way.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
You can enumerate the items in a HashTable like this:

        Dim ht As New HashTable ' (assuming there is stuff put in there somehow...)  
        Dim de As DictionaryEntry

        For Each de in ht
            ' display the key/value pair
            Debug.WriteLine("Key: " & de.Key)
            Debug.WriteLine("Value: " & de.Value)
        Next de

Then when you are all done you can empty the HashTable.  I don't see any need to remove items as you are working...

If you really want to delete as you enumerate then see my previous post.
OctalysAuthor Commented:
oh i saw the need to remove items is because, if I have 50 threads going tru that hashlist, it shouldnt process things twice. Thats why i thought removing it was the best.
OctalysAuthor Commented:
Ok what IF, this wasnt a hashlist but an Arraylist, would it makes thing easier?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
"if I have 50 threads going tru that hashlist, it shouldnt process things twice."

That's a whole different ball game my friend!  Enumerators are not thread safe....

To make this work you would have to sync-lock the HashTable (ArrayList, whatever it is...) so that each thread locks the structure when it reads the next record and then deletes it.  Otherwise you may have more than one thread read the same "next" record before it is deleted.

I don't have much experience with sync-locking code so I will let someone else give you an example of that.  I'm not even sure that multiple threads would work better than one since you will have a bottleneck waiting for the lock to release.  I guess it really depends on what you are doing with each record...
Diego PazosCommented:
foreach(O as object in HashTable.Values) {
    //Do stuff with O
}
SanclerCommented:
Standing back and looking at these two posts

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21471948.html

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21480327.html

I wonder whether the matter might be better viewed as a whole rather than in two bits.

From the first post it looks like there are two vary large lists of strings that need merging but without there being any duplicates.  The hashtable idea was introduced as a means of accomplishing that.

From the second post it looks like, having got the merged list, some operation has to be done on each item in it, but that item can then be discarded.  The order in which the items are dealt with does not seem to be important.

Going back to the first post again, at that stage the form of the two original very large lists was not settled "I am storing the links in a ?? string array I guesS? not sure where I would store it yet."

In the light of all this I wonder whether, first, storage could be in datatables that were susceptible to SQL querying.  The merging, without duplicates, could then be accomplished simply by a UNION query.

Then the processing of the resultant unique items list could be handled with reference to the datatable.  I'm not into multi-threading so I may be up the wall with this next point but, if different threads are going to be used, could that be covered with different (unique) rowfilters on the data which each is handling?

I'm posting this identically in both threads - is that multi-threading?

;-)

Roger
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.