How can I get the key for a specified value in a HashTable object?

G

George

Dear All,

I am having the following problem: I have a HashTable object and I would
like to extract the value for a specified key - the opposite is easy to do
with the get method, but extracting keys from values does not seem to be
an easy process. Any ideas?

Many thanks in advance.

George
 
F

Flo 'Irian' Schaetz

And thus spoke George...
I am having the following problem: I have a HashTable object and I would
like to extract the value for a specified key - the opposite is easy to do
with the get method, but extracting keys from values does not seem to be
an easy process. Any ideas?

That's not a good idea normaly - a HashTable is supposed to work the
other way round... But of course you can simple iterate through the
HashTable.keys() and see if one of them brings the correct value.

Flo
 
G

George

On Thu, 12 Jul 2007 Lekeas GK wrote...

|From: Flo 'Irian' Schaetz <[email protected]>
|Date: Thu, 12 Jul 2007 15:55:49 +0200
|Subject: Re: How can I get the key for a specified value in a HashTable
| object?
|
|And thus spoke George...
|
|> I am having the following problem: I have a HashTable object and I would
|> like to extract the value for a specified key - the opposite is easy to do
|> with the get method, but extracting keys from values does not seem to be
|> an easy process. Any ideas?
|
|That's not a good idea normaly - a HashTable is supposed to work the
|other way round... But of course you can simple iterate through the
|HashTable.keys() and see if one of them brings the correct value.

True, but this is not a very good idea when it comes down to searching
large hash tables. Is there any other data structure where there is no
key/value differentiation and you can search in both directions? I need to
be able to do that for the implementation of my PhD model and I have no
idea how to go about it.

Thanks,
George

|
|Flo
|
 
F

Flo 'Irian' Schaetz

And thus spoke George...
True, but this is not a very good idea when it comes down to searching
large hash tables.
Bingo.

Is there any other data structure where there is no
key/value differentiation and you can search in both directions? I need to
be able to do that for the implementation of my PhD model and I have no
idea how to go about it.

I don't know any bidirectional class, but you can simply build your
own... Extends HashTable and add another internal HashTable to it, where
you always save the other way, too:

public Object put(Object key, Object value) {
Object o = super(key, value);
otherTable.put(value, key);
return o;
}

so you could do...

public Object getKey(Object value) {
return otherTable.get(value);
}

Just the basic idea, of course. As HashTable needs unique keys, your
values will have to be unique, too, probably. Hm, not sure if there IS a
Hash-structure in Java that allows the same key to be added twice
(instead of simply overwriting the first) - it's not that hard to code,
but I never encountered it in Java...

Flo
 
L

Lew

And thus spoke George.......
Extends HashTable and add another internal HashTable to it, where
....

What is this class HashTable? Is it a custom class or a misspelling of
"Hashtable"? If the former, does it implement java.util.Map? If the latter,
why use Hashtable, some eight years or so obsolete, in lieu of HashMap or
other more biddable classes? Even if you require synchronization, the
Collections framework (specifically java.util.Collections) provides a better
way of getting it.

Are you guaranteed that the key is unique for the value? The reciprocal of a
function isn't always a function.
 
P

Patricia Shanahan

George said:
On Thu, 12 Jul 2007 Lekeas GK wrote...

|From: Flo 'Irian' Schaetz <[email protected]>
|Date: Thu, 12 Jul 2007 15:55:49 +0200
|Subject: Re: How can I get the key for a specified value in a HashTable
| object?
|
|And thus spoke George...
|
|> I am having the following problem: I have a HashTable object and I would
|> like to extract the value for a specified key - the opposite is easy to do
|> with the get method, but extracting keys from values does not seem to be
|> an easy process. Any ideas?
|
|That's not a good idea normaly - a HashTable is supposed to work the
|other way round... But of course you can simple iterate through the
|HashTable.keys() and see if one of them brings the correct value.

True, but this is not a very good idea when it comes down to searching
large hash tables. Is there any other data structure where there is no
key/value differentiation and you can search in both directions? I need to
be able to do that for the implementation of my PhD model and I have no
idea how to go about it.

Why not just build a two-way map using two HashMap instances, with the
key and value roles reversed between the two maps?

Patricia
 
F

Flo 'Irian' Schaetz

And thus spoke Lew...
What is this class HashTable? Is it a custom class or a misspelling of
"Hashtable"?

Probably the later. The finder of a typo may keep and frame it for
future generations to come :)
If the former, does it implement java.util.Map? If the latter,
why use Hashtable, some eight years or so obsolete, in lieu of HashMap or
other more biddable classes?

Details :). A good hint (personally, I normaly use HashMap), but
slightly Off-Topic. To be honest I didn't even think much about the name
of the class, because it didn't seem to be interessting.
Are you guaranteed that the key is unique for the value? The reciprocal of a
function isn't always a function.

Is this a reply to my posting? Doesn't make much sense, because I
mentioned that this could be a problem... Probably would be better as a
reply to the OP.

Flo
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Patricia Shanahan schreef:
Why not just build a two-way map using two HashMap instances, with the
key and value roles reversed between the two maps?

Or use one of the BidiMap implementations which are already provided in
Jakarta Commons Collections (one of them does just that). If you want
it to be generic, give me a note and I’ll send you a generified version.

Cheers, H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGlkE+e+7xMGD3itQRApCAAJwLO/2WMvoxXEFlrSS+Cin+qMbwOgCbBY0F
hodKzc5/dGGkFFicZhjHIOg=
=vltz
-----END PGP SIGNATURE-----
 
E

Eric Sosman

George wrote On 07/12/07 10:03,:
On Thu, 12 Jul 2007 Lekeas GK wrote...

|From: Flo 'Irian' Schaetz <[email protected]>
|Date: Thu, 12 Jul 2007 15:55:49 +0200
|Subject: Re: How can I get the key for a specified value in a HashTable
| object?
|
|And thus spoke George...
|
|> I am having the following problem: I have a HashTable object and I would
|> like to extract the value for a specified key - the opposite is easy to do
|> with the get method, but extracting keys from values does not seem to be
|> an easy process. Any ideas?
|
|That's not a good idea normaly - a HashTable is supposed to work the
|other way round... But of course you can simple iterate through the
|HashTable.keys() and see if one of them brings the correct value.

True, but this is not a very good idea when it comes down to searching
large hash tables. Is there any other data structure where there is no
key/value differentiation and you can search in both directions? I need to
be able to do that for the implementation of my PhD model and I have no
idea how to go about it.

Any reason you can't just use two Hashtables (or HashMaps)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top