电子表格数据 - 链表还是哈希图?
我正在寻找用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
另请查看 Guava 的表和实现类。
Also take a look at Guava's Table and implementing classes.
使用
Map
可以让您更快、更轻松地查找值。您不需要多个数据结构,一个HashMap
就可以了。例如,你可以这样做:-Using
Map
may allow you to lookup the values faster and easier. You don't need more than one data structure, a singleHashMap
will do. For example, you could do something like this:-在设计此类问题的数据结构时,从定义 SpreadSheet 的接口开始会更容易。一旦定义了所有操作(读操作、创建操作、修改操作和删除操作),数据结构所需的特性(顺序访问、直接访问等)就会变得明显。
在电子表格中,需要一种机制来使用需要映射的索引/名称来直接访问列、行或单元格。还需要对行和列进行顺序访问(迭代),这需要列表。因此,您需要一个 MapList 接口。现在我们需要选择实现。由于删除可能发生在行或列列表中的任何位置,因此 LinkedList 实现似乎是最好的。它还允许对列表重新排列进行恒定时间操作。总而言之,LinkedListMap 将是行和列的最佳选择。
class SpreadSheet:
class Row:
class Cell:
class Col:
从工作表访问特定单元格:
使用上述数据结构,您应该能够轻松实现复杂的操作,例如获取子电子表格(矩形选择)。
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:
class Row:
class Cell:
class Col:
To access a particular cell from sheet:
With the above data structures, you should be able to easily implement even complex operations like getting a sub-spreadsheet (a rectangular selection).
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.
取决于您打算使用此电子表格做什么 - 如果仅迭代 - 那么链接列表会适合,但我真的怀疑电子表格是否需要使用它。为了实现对单元的快速且可扩展的访问,您应该使用带有内部哈希映射的 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.