2D lookup table

M

Mize-ze

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?

Example:

If I have a list of doubles and I want to store a value per each pairs
of values
What is the best way to do this?


Thanks
 
A

Andreas Leitgeb

Mize-ze said:
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.
 
E

Eric Sosman

Mize-ze wrote On 01/31/07 12:16,:
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?

Example:

If I have a list of doubles and I want to store a value per each pairs
of values
What is the best way to do this?

I don't know of any standard class(es) to do this. You
could use a HashMap, but if the values you search for are
even the teensiest bit different from those you inserted,
you won't find them. That is, if the matrix has an entry
for (1.0,2.0) but the calculations that produce the search
pair produce (1.00000000000000021,1.99999999999999987), the
tiny numerical errors will cause the search to come up empty.

You need (I think) some kind of "proximity search" or
"nearest match" search that will be insensitive to the small
inaccuracies that creep into floating-point calculations. A
binary tree might work, comparing X coordinates on the even
levels and Y coordinates on the odds, and searching both
branches if the target value is "very close" to the tree's
dividing value.

Or perhaps what you need is interpolation in a mesh?
Best bet there, I think, is to search each coordinate axis
to find the two values that bracket the search coordinate,
pluck out the four "corner" values, and interpolate in that
rectangular patch. Again, I don't think Java supplies any
built-in class for the job.
 
?

=?iso-8859-2?B?QXJ0dXIgQ2h5v3k=?=

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?

Example:

If I have a list of doubles and I want to store a value per each pairs
of values
What is the best way to do this?

Thanks

I recommend trying jakarta commons collections library.
There is a MultiKey class which you may find useful.
Also remember taht simplest solutions are always the best and hardest
to find.
Map of maps is a little bit complicated solution. You can achieve this
using MultiKey
From API :
Example usage:

// populate map with data mapping key+locale to localizedText
Map map = new HashMap();
MultiKey multiKey = new MultiKey(key, locale);
map.put(multiKey, localizedText);

// later retireve the localized text
MultiKey multiKey = new MultiKey(key, locale);
String localizedText = (String) map.get(multiKey);

I think also that using tranlation table and using bigger table will
be faster than working on maps (it also depends how many objects you
want to store).
If you make some way to translate indexes from int to your own value
it would be simple and fast.

Good luck in finding the solution.
Arthur
 
D

Daniel Pitts

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?

Example:

If I have a list of doubles and I want to store a value per each pairs
of values
What is the best way to do this?

Thanks
Well, it kind of depends on the situation.
You can't reliably use doubles as a key to anything. A pair of doubles
is, *ahem*, doublely unreliable.
But, if you have a String as an x coordinate, and a String as the y
coordinate, then you have a few options.

If there is a finite set of x coordinates and a finite set of y
coordinates, and the matrix is "mostly" full, then you would be best
to use a translation table.
Map<String, Integer> xCoordinates;
Map<String, Integer> yCoordinates;

Object[][] matrix = new Object[yCoordinates.size()]
[xCoordinates.size()];

Alternatively, if your matrix is "mostly" empty, you may wish to use
nested maps:

Map<String, Map<String, Object>> matrix;

As another alternative, you may want to use a different "structure"
altogether:

final class Coordinate<X, Y> {
final X x;
final Y y;
public Coordinate(X x, Y y) {
this.x = x;
this.y = y;
}

public boolean equals(Object o) {
if (o == null || !(o instanceof Coordinate)) {
return false;
}
Coordinate c = (Coordinate)o;
return c.x.equals(x) && c.y.equals(y);
}

public int hashCode() {
return x.hashCode() * 39 + y.hashCode();
}
}


Map<Coordinate, Object> matrix;


Hope this helps,
Daniel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top