views:

46

answers:

2

I need to represent data (retreived from a db) representing tabular data (with an x and y axis):

The pair (x,y) has data (an object) associated with it. What would be the most semantically correct way to represent this structure? Of course I have to retrieve the data by indexing either by x or by y, but a symmetric way to represent this would be great.

The difficulty is that x and y alone do not map to the data object, by the pair (x,y) does. I have come up with things like a map from x to another map with y as the key and the data object as the value..but this is not easily invertible and is not very symmetric.

Thanks

+1  A: 

Have you considered something like this?

class MyPoint {
    int x;
    int y;
    Object data;
    // setters/getters go below
}

You can use sql (or hibernate/jpa) to retrieve data by x or y coordinate.
Personally, I don't think that 'indexing' should be a part of data structure: database will handle it perfectly without your help.

Nikita Rybak
Sorry - I didnt mean 'indexing' in database terms (I just meant retreivign from the db in row major vs column major). What if I want to be able to order the rows/columns in a certain way when displaying this table? Assuming all I have is a list of MyPoint objects, isnt that difficult? THanks
ewa
@ewa _I just meant retreivign from the db in row major vs column major_ Then you just make hibernate query, something like "from MyPoint where x = 23 order by y". So, if you use db as storage space, it's really easy. But if you have only list of all MyPoint objects, then you'd need some other structures.
Nikita Rybak
Yea. Another thing thats difficult is actually populating the table since it will be some sort of nested for loop over the i and j. So i probably need something in addition to what you suggested
ewa
A: 

Your tabular data, i guess, should be in the form of some function, for which f(x) = y, if that's the case I think a HashMap would be ok.

As your question about symmetry, I think you could implement some class

public class Pair{
   private long id;
   private int x;
   private int y;
   private Data data;
}

An then you could store it in some other structure you consider appropriate. As of your indexing, I don't know if that would be a good Idea, given that you can have duplicate values of a given x,y pair for different data sets.

StudiousJoseph