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 / First Aid / August 2007

Tip: Looking for answers? Try searching our database.

How can i implements a class like an HashMap but with 2 keys ?

Thread view: 
Rob - 28 Aug 2007 13:02 GMT
Hi all,
i am a newbie,  i want to create a class that works like an HashMap but
with 2 keys,  get(key1,key2) -> value

I was thinking to create a class "Pair" that override equals and hashcode
and then store here the two keys and then a class that holds an HashMap
with Pair as key and the value class, can be this a solution?  or what can
i use?

Bye thanks for the help
Jan Thomä - 28 Aug 2007 13:28 GMT
> i want to create a class that works like an HashMap but
> with 2 keys,  get(key1,key2) -> value
>
> I was thinking to create a class "Pair" that override equals and hashcode
> and then store here the two keys and then a class that holds an HashMap
> with Pair as key and the value class, can be this a solution?

Sounds good to me, actually you could just use a plain hashmap with that
pair class as key, no need to wrap it inside another class. What you could
also do is generating a string from the two keys which is then used as
combined key, but the pair thing looks cleaner to me...

Best regards,
Jan Thomä
Signature

_________________________________________________________________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de

Lew - 28 Aug 2007 13:36 GMT
Rob wrote:
>> i [sic] want to create a class that works like an HashMap but
>> with 2 keys,  get(key1,key2) -> value
>> I was thinking to create a class "Pair"
>> that override equals and hashcode [sic]

That's "hashCode".

>> and then store here the two keys and then a class that holds an HashMap
>> with Pair as key and the value class, can be this a solution?

This would be the canonical solution.  Make Pair immutable and generic if your
keys are of general type, non-generic if you know what the key types are.

Signature

Lew

Hendrik Maryns - 28 Aug 2007 15:53 GMT
Jan Thomä schreef:
>> i want to create a class that works like an HashMap but
>> with 2 keys,  get(key1,key2) -> value
[quoted text clipped - 7 lines]
> also do is generating a string from the two keys which is then used as
> combined key, but the pair thing looks cleaner to me...

Have a look at Jakarta Commons Collections, it has MultiKey and
MultiKeyMap, which works exactly as you describe.

If you want a generic version, let me know.

H.
Signature

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

Rob - 28 Aug 2007 17:31 GMT
> Have a look at Jakarta Commons Collections, it has MultiKey and
> MultiKeyMap, which works exactly as you describe.
>
> If you want a generic version, let me know.
>
> H.

Yes, i read in the api and seems perfect for what i have to do.

A generic version is better, do you know where can i get it?

Thanks
Hendrik Maryns - 29 Aug 2007 12:18 GMT
Rob schreef:

>> Have a look at Jakarta Commons Collections, it has MultiKey and
>> MultiKeyMap, which works exactly as you describe.
[quoted text clipped - 6 lines]
>
> A generic version is better, do you know where can i get it?

The Jakarta team has been very reluctant in starting a generic version.
Finally, there has been some advancement there.  However, I have made
my own generic version, if you send me an e-mail, I can send it to you.

Mention which classes you would like.

Alternatively, there is ‘larvalabs’.

H.
Signature

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

Roedy Green - 28 Aug 2007 23:38 GMT
>i am a newbie,  i want to create a class that works like an HashMap but
>with 2 keys,  get(key1,key2) -> value

Here are three  solutions:

1. use two HashMaps on the same set of objects. Then you can look up
by either key.

2. create a Pair class that contains key1 and key2 with an ^ (xor)
combined hashCode.  Then Pair is a single key for both building and
lookup.  You need both keys to lookup, like a safety deposit box.

3. concatenate your two String keys into one.

See http://mindprod.com/jgloss/hashcode.html
http://mindprod.com/jgloss/hashmap.html
http://mindprod.com/jgloss/hashtable.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Patricia Shanahan - 29 Aug 2007 14:10 GMT
>> i am a newbie,  i want to create a class that works like an HashMap but
>> with 2 keys,  get(key1,key2) -> value
...
> 3. concatenate your two String keys into one.

I'd avoid this one unless there is at least one char bit pattern that
cannot appear in the first input, to use as separator.

For example, using "_" for the separator, the key "A_B_C" would be used
for both ("A_B","C") and ("A","B_C"), making it impossible to
distinguish those pairs.

This can be worked around by an encoding step before the concatenation,
but that makes it all more complicated, and simplicity is the main merit
of this method.

Patricia
Gordon Beaton - 29 Aug 2007 14:24 GMT
>> 3. concatenate your two String keys into one.
>
> I'd avoid this one unless there is at least one char bit pattern
> that cannot appear in the first input, to use as separator.

Why is it necessary to separate them? The cost of not doing so is only
an increased risk of collisions.

In fact that cost might be zero or so negligible that adding the
separator at each operation costs more than the potential collisions
it attempts to avoid.

I'm assuming that the concatenation is only used internally in the
HashMap, and that the separate components are used elsewhere in their
original, separate state.

/gordon

--
Patricia Shanahan - 29 Aug 2007 14:36 GMT
>>> 3. concatenate your two String keys into one.
>> I'd avoid this one unless there is at least one char bit pattern
[quoted text clipped - 10 lines]
> HashMap, and that the separate components are used elsewhere in their
> original, separate state.

If it were only for purposes of calculating hashCode(), I would
completely agree. However, my understanding is that this is to be used
as the key in a single key HashMap substituting for a map keyed by the
pair.

In the portion I snipped, the first two items in the list were:

>> 1. use two HashMaps on the same set of objects. Then you can look up
>> by either key.
>>
>> 2. create a Pair class that contains key1 and key2 with an ^ (xor)
>> combined hashCode.  Then Pair is a single key for both building and
>> lookup.  You need both keys to lookup, like a safety deposit box.

Both of those solutions would treat <"A_B",C> and <"A","B_C"> as
different keys. Both could be present as keys, with different values, at
the same time.

Patricia
Gordon Beaton - 29 Aug 2007 14:41 GMT
> If it were only for purposes of calculating hashCode(), I would
> completely agree. However, my understanding is that this is to be used
> as the key in a single key HashMap substituting for a map keyed by the
> pair.

Thanks. My thoughts were stuck on hashCode() when I posted earlier, I
forgot about (in)equality of the keys themselves.

/gordon

--
Gordon Beaton - 29 Aug 2007 14:37 GMT
> Why is it necessary to separate them? The cost of not doing so is only
> an increased risk of collisions.

Please ignore that. After posting I realized that the concatenated
keys themselves need to be unique, not the hashcodes...

/gordon

--
Roedy Green - 30 Aug 2007 10:49 GMT
>Why is it necessary to separate them? The cost of not doing so is only
>an increased risk of collisions.

You could get false hits:

applespear  would match apple-spear and apples-pear
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.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



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