开发者

Spreadsheet data - linked list or hashmap?

开发者 https://www.devze.com 2023-02-11 22:46 出处:网络
I am looking to implement a spreadsheet in java. Would it be better to use a linked lis开发者_Go百科t (rows) of linked list of cells (columns) to store the data or a hashmap (each cell maps to a key e

I am looking to implement a spreadsheet in java. Would it be better to use a linked lis开发者_Go百科t (rows) of linked list of cells (columns) to store the data or a hashmap (each cell maps to a key e.g. A1 -> 1, A2-> 2 etc.)?

Or is there an even better data structure to use?

Thanks!


Also take a look at Guava's Table and implementing classes.


Using Map may allow you to lookup the values faster and easier. You don't need more than one data structure, a single HashMap will do. For example, you could do something like this:-

public class Location {
    private Integer x;
    private Integer y;

    public Location(Integer x, Integer y) {
        this.x = x;
        this.y = y;
    }

    // implement the equals and hashcode methods
}

public class MySpreadSheet {

    private Map<Location, String>   spreadsheet = new HashMap<Location, String>();

    public String getCellValue(Integer x, Integer y) {
        return spreadsheet.get(new Location(x, y));
    }

    public void setCellValue(Integer x, Integer y, String value) {
        spreadsheet.put(new Location(x, y), value);
    }
}


When designing the data structure for such problems, its easier to start by defining the interface for SpreadSheet. Once all the operations (read operations, create operations, modify operations and delete operations) are defined, the characteristics required (sequential access,direct access etc.) of the data structure would become obvious.

In a spreadsheet, one would need mechanisms to directly access columns, rows or cells using indices/names which calls for a Map. One would also need sequential access (iteration) over rows and columns which calls for a List. So a MapList interface is what you need. Now we need to choose the implementation. Since deletion can happen anywhere within the list of rows or columns, a LinkedList implementation seems best. It would also allow constant time operations for list re-arranging. Summing up, a LinkedListMap would be the best choice for rows and columns.

class SpreadSheet:

LinkedListMap<String,Row> rows; 
// RowKey --> Row. Row has data. This allows direct access and looping.
LinkedListMap<String,Col> cols; 
//only metadata - name,sort status, visible/invisible...
//This allows direct access and looping.

class Row:

LinkedListMap<String,Cell> cells; //colKey --> cell
//This allows direct access and looping. 

class Cell:

Object value;
EType dataType; //enum for data type

class Col:

String name;
ESortState sortState; //ASC, DESC, NONE
boolean visible; 

To access a particular cell from sheet:

rows.get(rowKey).getValue(cells.get(colKey).getName())

With the above data structures, you should be able to easily implement even complex operations like getting a sub-spreadsheet (a rectangular selection).


A HashMap as a base data structure (I say base because you could combine multiple structures to get you the most optimal storage) would probably be the best solution. If you needed to access one of the cells and you implemented a linked list, it would require O(n) time to iterate the list to get to that cell, whereas a HashMap would only require O(1) time to access what you want. The same would go for insertions and deletions into the data structure.


Depends on what you aim to do with this spreadsheet - if iterations ONLY - then linked lists would suit, but I really doubt, that the spreadsheet is necessary to use this. To achieve the fast and scalable access to cells you should use HashMap with inner hash maps. Do not forget to preset the sizes of these maps, if your spreadsheet columns are not dynamic and you know its number exactly.

0

精彩评论

暂无评论...
验证码 换一张
取 消