Server Help

Trash Talk - java hashtables: find a key for a value

Helicon - Sat Nov 08, 2003 11:10 pm
Post subject: java hashtables: find a key for a value
alright.. i've done java for years but have yet to see a good solution for this one:

i have a java.util.Hashtable with a value added to it
Code: Show/Hide
Hashtable h = new Hashtable();
Vector vect = new Vector();
h.put("Main",v);

What i need to do is dynamically retrieve "Main" given vect.
here is the javadoc for Hastable (JDK 1.4.2)
http://java.sun.com/j2se/1.3/docs/api/java/util/Hashtable.html
i can obviously use:
Code: Show/Hide
Enumeration e = h.keys();
while(h.hasMoreElements()){
//... iterate, checking to see if this works:
String temp = (String)e.nextElement();
if(h.get(temp)!=null)
return temp;
}

but that is rediculous...
Am i missing something obvious????
Dr Brain - Sun Nov 09, 2003 12:10 am
Post subject:
Not really.

You could of course have two hashmaps/hashtables and use the keys of one as the objects of another.

Why do you need to do this?
Helicon - Sun Nov 09, 2003 3:51 pm
Post subject:
Dr Brain wrote:
Why do you need to do this?

i'm not a moron... i know when i need to use a hashtable.
The trouble here is that somewhere down the hierarchy there are vectors(hence arrays) containing the keys which yield the hashes and the objects themselves. It would be simple, given these, to just match up indices and return either. However, i don't feel like extending my way down the hierarchy to accomplish this. It just seems like having to iterate twice to find objects which reside at the same index is a little screwy... perhaps i'm wrong about the indices... it may be by hash, but that would be just a small drag compared to an iteration... my collections don't have defined limits in this case... that would severely bite my already lagging java performance
Cyan~Fire - Sun Nov 09, 2003 3:54 pm
Post subject:
Asking you what for doesn't necessarily mean you're a moron. There's just more efficient ways of doing things depending on what you need them for...
Helicon - Sun Nov 09, 2003 3:56 pm
Post subject:
Cyan~Fire wrote:
There's just more efficient ways of doing things depending on what you need them for...

like not using java, tee hee
Dr Brain - Sun Nov 09, 2003 8:44 pm
Post subject:
Helicon wrote:
i'm not a moron... i know when i need to use a hashtable.
The trouble here is that somewhere down the hierarchy there are vectors(hence arrays) containing the keys which yield the hashes and the objects themselves. It would be simple, given these, to just match up indices and return either. However, i don't feel like extending my way down the hierarchy to accomplish this. It just seems like having to iterate twice to find objects which reside at the same index is a little screwy... perhaps i'm wrong about the indices... it may be by hash, but that would be just a small drag compared to an iteration... my collections don't have defined limits in this case... that would severely bite my already lagging java performance


I didn't say you were a moron, but usually there is a better way to do something when you are having trouble with a build in class.

I beleive that they arent stored in vectors, but rather in a linked list type thing, and a hash is used to position data in it.

If performance is a big deal, make your own. If its not, use two hash tables.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group