Data table related to tables or hash map?

I am looking to implement a table in java. Would it be better to use the linked list (rows) of the linked list of cells (columns) to store data or a hash map (each cell displays a key, for example A1 → 1, A2-> 2, etc.)?

Or is there an even better data structure?

Thank!

+5
source share
5 answers

Also consider a Guava table and implement classes.

+5
source

Map . , HashMap. , - : -

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);
    }
}
+2

SpreadSheet. , ( , , ), ( , ..) .

, /, Map. () , . , MapList - , . . , LinkedList . . , LinkedListMap .

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. 

Cell:

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

Col:

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

:

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

, - ( ).

+2

A HashMap ( , , , ), , . , , O (n), , , HashMap O (1), , , .

0

Depends on what you intend to do with this table - if iterations are ONLY - then linked lists will be matched, but I really doubt that a spreadsheet is needed to use it. To achieve fast and scalable access to cells, you must use the HashMap with internal hash maps. Do not forget to indicate the sizes of these maps, if your spreadsheet tables are not dynamic, and you know exactly its number.

0
source

All Articles