javadata-structuresstorein-memory

What would be an appropriate data-structure to store a 2D table structure in memory?


I have a 2D table structure that I am reading from an underlying database, and I am wondering, what is the best in-memory data-structure to use to store it into. Where, it can be accessible for further reading, manipulation, etc.


Solution

  • I would do maps of maps something like

    Map<String, Map<String,String>> table = new HashMap<String, Map<String,String>>();
    

    Here is an example

    column_a(primary),  column_b,  column_c
    --------------------------------
    abc              |  xyz      | 123
    def              |  xxx      | 456
    

    So after you read the table you use it like this:

    map.get("abc").get("column_b").equals("xyz")
    map.get("def").get("column_c").equals("456")
    

    Hope this helps. Note: I chose the first map to have a key of string because your table may not have integers as IDs. You can change them if you think Integer would fit better.