For Programmers: Free Programming Magazines  


Home > Archive > Java Help > August 2007 > How can i implements a class like an HashMap but with 2 keys ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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

2007-08-28, 8:41 am


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ä

2007-08-28, 8:41 am

Rob wrote:
> 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ä
--
________________________________________
_________________________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de

Rob

2007-08-28, 7:17 pm

Hendrik Maryns wrote:

>
> 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
Roedy Green

2007-08-28, 7:17 pm

On Tue, 28 Aug 2007 14:02:21 +0200, Rob <nomail@nomail> wrote, quoted
or indirectly quoted someone who said :

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

2007-08-29, 7:11 pm

On Wed, 29 Aug 2007 06:10:46 -0700, Patricia Shanahan wrote:
> Roedy Green wrote:
>
> 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

--
Gordon Beaton

2007-08-29, 7:11 pm

On 29 Aug 2007 13:24:31 GMT, Gordon Beaton wrote:
> 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

--
Gordon Beaton

2007-08-29, 7:11 pm

On Wed, 29 Aug 2007 06:36:43 -0700, Patricia Shanahan wrote:
> 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

--
Roedy Green

2007-08-30, 8:12 am

On 29 Aug 2007 13:24:31 GMT, Gordon Beaton <n.o.t@for.email> wrote,
quoted or indirectly quoted someone who said :

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







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com