Link to home
Start Free TrialLog in
Avatar of Octalys
Octalys

asked on

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
Avatar of doobdave
doobdave

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
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)
Avatar of Bob Learned
It sounds like a hash table is not the best weapon of choice :)

Bob
Avatar of Octalys

ASKER

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

Bob
Avatar of Octalys

ASKER

[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.
Ouch!!!

Bob
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Octalys

ASKER

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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
BTW, using the enumerator with MoveNext is by it's very nature sequential.

Bob
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...
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
@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
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
Avatar of Octalys

ASKER

Well the function does this first.

https://www.experts-exchange.com/questions/21471948/I-have-two-very-large-list-of-strings-and-I-am-comparing-list2-with-list1-it-need-to-merge-but-duplicates-are-not-allowed-how-do-I-do-this.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.
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?
Avatar of Octalys

ASKER

well I am working with threads to process the hasttable, i thought removing items from the hashtable is the simplest/best way.
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.
Avatar of Octalys

ASKER

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.
Avatar of Octalys

ASKER

Ok what IF, this wasnt a hashlist but an Arraylist, would it makes thing easier?
"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...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial