Hi,
I need to convert something, say A1 to b1, B1 to c1, C1 to d1, before
calculation. After calculation is done, I need to convert them back to
its original values, that is b1 to A1, c1 to B1, d1 to C1. It sounds
like I need a two-way mapping Map. Is such a data structure available in
Java?
Of course, I can use two separate Map<String, String>.
First map:
A1-->b1
B1-->c1
C1-->d1
Second map:
b1-->A1
c1-->B1
d1-->C1
I am just curious if there is one Map can do all above.
Another related, but more difficult question is that:
suppose that there are four columns: column A, column B, column C, and
column D. At one time, I need to map from column A to column C. Other
time, I need to map from column A to column B. Sometime I need to map
from column C to column D and from column C to column A. For example,
suppose one row of the four columns are:
"rain", "inch", "precipitation", "mm"
Sometimes, I need to use "rain" as a key to retrieve the column C value,
"precipitation"; sometimes, I need to use "rain" as a key to retrieve
the colunm B value, "inch"; sometimes, I need to use "precipitation" as
a key to retrieve the column A value, "rain"; sometimes, I need to use
"precipitation" as a key to retrieve the colunm D value, "mm".
Is some data structure that allows me do that?
Thank you very much.
Matt Humphrey - 08 Apr 2008 14:41 GMT
> Hi,
>
[quoted text clipped - 15 lines]
>
> I am just curious if there is one Map can do all above.
If your map is truly reversible, just put the values back in as keys, e.g.
A1-->b1
b1-->A1
Or use a BidiMap
http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/Bid
iMap.html
> Another related, but more difficult question is that:
>
[quoted text clipped - 13 lines]
>
> Is some data structure that allows me do that?
Are your terms all unique within their columns? If they are, it seems
relatively easy to produce a structure for this. You might try to search
for pivot table, although that is a different kind of thing. Also, it may
be easier to think of this has being one table with 4 indexes, each that map
from the key to the row. Once you have the row you just pick off the value
you want. In my experience, the real problem tends to be that the values are
not unique within their columns.
Matt Humphrey http://www.iviz.com/
Andreas Leitgeb - 08 Apr 2008 15:35 GMT
> Of course, I can use two separate Map<String, String>.
> First map:
[quoted text clipped - 6 lines]
> c1-->B1
> d1-->C1
In this example, you could of course join these two maps,
since they do not seem to share any keys at all.
> [ A B C D ]
> "rain", "inch", "precipitation", "mm"
[quoted text clipped - 3 lines]
> a key to retrieve the column A value, "rain"; sometimes, I need to use
> "precipitation" as a key to retrieve the colunm D value, "mm".
Can there be collisions of words in different columns?
(Is there a word, that can appear in two columns?)
if not, then you need one map per target-column:
MapA: {rain rain inch rain precipitation rain mm rain}
MapB: {rain inch inch inch precipitation inch mm inch}
...
Eventually, you can leave out the identical-mappings.
> Is some data structure that allows me do that?
arrays seem just fine for the purpose.
Andreas Leitgeb - 08 Apr 2008 15:41 GMT
> if not, then you need one map per target-column:
> MapA: {rain->rain inch->rain precipitation->rain mm->rain}
> MapB: {rain->inch inch->inch precipitation->inch mm->inch}
> Eventually, you can leave out the identical-mappings.
>> Is some data structure that allows me do that?
> arrays seem just fine for the purpose.
waaah, I was interrupted during writing of this post, and
when I finished it, I thought I was in tcl-newsgroup
Sorry, for confusion. ("array" is just tcl's term for
HashMap, which I really meant.)
Lew - 09 Apr 2008 00:45 GMT
>> Of course, I can use two separate Map<String, String>.
>> First map:
[quoted text clipped - 9 lines]
> In this example, you could of course join these two maps,
> since they do not seem to share any keys at all.
It gets trickier if the key and value types differ.

Signature
Lew