Re: 2D lookup table
- From: Andreas Leitgeb <avl@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: 31 Jan 2007 17:32:59 GMT
Mize-ze <zahy.bnaya@xxxxxxxxx> wrote:
If I want to store and fetch information from a matrix like structure
where the indexes of the matrix are not integers but other data
structures, how such thing can be done without a translation table?
well, you can of course use a HashMap that has again a HashMap
as it's Value-type, like:
Map<String,Map<String,Double>> map2d=
new HashMap<String,Map<String,Double>>();
For each new "x-coordinate", you'd have to instanciate
a new sub-HashMap, and put it into map2d. This could all be
wrapped in some new class.
to retrieve an element, you just use:
map2d.get(xKey).get(yKey)
(adding NullPointer-Checks is left as an exercise :-)
Alternatively, you could create a "Pair" class, and use that
as the Key-type of a HashMap. Don't forget to overwrite
..equals() and .hashCode() for your Pair class, or you'll
have problems getting your elements back.
PS: of course, you can use any type of Map, not just HashMap.
.
- References:
- 2D lookup table
- From: Mize-ze
- 2D lookup table
- Prev by Date: Re: Vector Storing
- Next by Date: Re: Sorting a vector based on another datas
- Previous by thread: 2D lookup table
- Next by thread: Re: 2D lookup table
- Index(es):