电子表格数据 - 链表还是哈希图?

发布于 2024-10-17 23:48:33 字数 134 浏览 2 评论 0原文

我正在寻找用java实现电子表格。使用单元格(列)链接列表的链接列表(行)来存储数据或哈希图(每个单元格映射到一个键,例如 A1 -> 1、A2-> 2 等)会更好吗?

或者有更好的数据结构可以使用吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

通知家属抬走 2024-10-24 23:48:33

另请查看 Guava 的表和实现类。

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

傲影 2024-10-24 23:48:33

使用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);
    }
}

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);
    }
}
寄与心 2024-10-24 23:48:33

在设计此类问题的数据结构时,从定义 SpreadSheet 的接口开始会更容易。一旦定义了所有操作(读操作、创建操作、修改操作和删除操作),数据结构所需的特性(顺序访问、直接访问等)就会变得明显。

在电子表格中,需要一种机制来使用需要映射的索引/名称来直接访问列、行或单元格。还需要对行和列进行顺序访问(迭代),这需要列表。因此,您需要一个 MapList 接口。现在我们需要选择实现。由于删除可能发生在行或列列表中的任何位置,因此 LinkedList 实现似乎是最好的。它还允许对列表重新排列进行恒定时间操作。总而言之,LinkedListMap 将是行和列的最佳选择。

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; 

从工作表访问特定单元格:

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

使用上述数据结构,您应该能够轻松实现复杂的操作,例如获取子电子表格(矩形选择)。

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).

梦亿 2024-10-24 23:48:33

HashMap 作为基础数据结构(我说基础是因为你可以组合多个结构以获得最佳存储)可能是最好的解决方案。如果您需要访问其中一个单元格并实现了一个链表,则需要 O(n) 时间来迭代列表才能到达该单元格,而 HashMap 只需要 O(1) 时间即可访问您想要的内容。数据结构的插入和删除也是如此。

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.

黎歌 2024-10-24 23:48:33

取决于您打算使用此电子表格做什么 - 如果仅迭代 - 那么链接列表会适合,但我真的怀疑电子表格是否需要使用它。为了实现对单元的快速且可扩展的访问,您应该使用带有内部哈希映射的 HashMap。如果您的电子表格列不是动态的并且您确切知道其数量,请不要忘记预设这些地图的大小。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文