Author |
Message |
Helicon Server Help Squatter
Joined: Dec 03 2002 Posts: 771 Location: GNU Doldrums Offline
|
Posted: Sat Nov 08, 2003 11:10 pm Post maybe stupid 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
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:
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???? _________________ Signatures just seem so quaint. |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Sun Nov 09, 2003 12:10 am Post maybe stupid 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? _________________ Hyperspace Owner
Smong> so long as 99% deaths feel lame it will always be hyperspace to me |
|
Back to top |
|
 |
Helicon Server Help Squatter
Joined: Dec 03 2002 Posts: 771 Location: GNU Doldrums Offline
|
Posted: Sun Nov 09, 2003 3:51 pm Post maybe stupid 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 |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Sun Nov 09, 2003 3:54 pm Post maybe stupid 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... _________________ This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him. |
|
Back to top |
|
 |
Helicon Server Help Squatter
Joined: Dec 03 2002 Posts: 771 Location: GNU Doldrums Offline
|
Posted: Sun Nov 09, 2003 3:56 pm Post maybe stupid 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 |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Sun Nov 09, 2003 8:44 pm Post maybe stupid 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. |
|
Back to top |
|
 |
|