Tutorial :data structure for tabular data



Question:

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


Solution:1

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.


Solution:2

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »