Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spreadsheet data - linked list or hashmap?

I am looking to implement a spreadsheet in java. Would it be better to use a linked list (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!

like image 295
Java User Avatar asked Feb 19 '11 18:02

Java User


2 Answers

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

like image 110
superfav Avatar answered Oct 01 '22 14:10

superfav


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);
    }
}
like image 38
limc Avatar answered Oct 01 '22 15:10

limc