Home > Archive > Java Help > August 2007 > Re: 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 |
Re: How can i implements a class like an HashMap but with 2 keys
|
|
| Patricia Shanahan 2007-08-29, 8:11 am |
| Roedy Green wrote:
> On Tue, 28 Aug 2007 14:02:21 +0200, Rob <nomail@nomail> wrote, quoted
> or indirectly quoted someone who said :
>
....[color=darkred]
> 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
| |
| Patricia Shanahan 2007-08-29, 7:11 pm |
| Gordon Beaton wrote:
> On Wed, 29 Aug 2007 06:10:46 -0700, Patricia Shanahan wrote:
>
> 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.
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:
[color=darkred]
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
|
|
|
|
|