Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
George - 12 Jul 2007 14:47 GMT
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
Flo 'Irian' Schaetz - 12 Jul 2007 14:55 GMT
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
George - 12 Jul 2007 15:03 GMT
On Thu, 12 Jul 2007 Lekeas GK wrote...

|From: Flo 'Irian' Schaetz <iryan@gmx.de>
|Date: Thu, 12 Jul 2007 15:55:49 +0200
[quoted text clipped - 11 lines]
|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
Flo 'Irian' Schaetz - 12 Jul 2007 15:12 GMT
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
Lew - 12 Jul 2007 15:24 GMT
And thus spoke George...
>> I am having the following problem: I have a HashTable object and I would
...

> 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.

Signature

Lew

Flo 'Irian' Schaetz - 12 Jul 2007 15:51 GMT
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
Patricia Shanahan - 12 Jul 2007 15:33 GMT
> On Thu, 12 Jul 2007 Lekeas GK wrote...
>
[quoted text clipped - 19 lines]
> 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
Hendrik Maryns - 12 Jul 2007 15:57 GMT
Patricia Shanahan schreef:
>> On Thu, 12 Jul 2007 Lekeas GK wrote...
>>
[quoted text clipped - 26 lines]
> 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
Eric Sosman - 12 Jul 2007 17:09 GMT
George wrote On 07/12/07 10:03,:
> On Thu, 12 Jul 2007 Lekeas GK wrote...
>
[quoted text clipped - 19 lines]
> 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)?

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.