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!
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With