Dynamic table / matrix data structure for Java
I need a Java implementation of t开发者_高级运维able-like data structure where I could dynamically insert or delete rows and columns. I need to get data from any row or column very fast and with no overhead in selecting row over column or vice versa.
Does anyone know libraries where such data structure is already implemented?
You might be able to use the DefaultTableModel. It was intended to be used with a JTable, but there is no reason it can't be used alone. You would need to add methods to retrieve the data for a full row or column.
If the performance is critical, you can use a 2D-array, and implement a reallocation algorithm (e.g. doubling) so that it can grow.
HashBasedTable class from Google Guava libraries does this. There is also TreeBasedTable if rows need to be in sorted order.
Perhaps JQL or HSQL DB
You could simply use List<List<YourClass>>
. Or, even simpler Map<Integer, List<YourClass>>
mapping the row number (first parameter, Integer) to a row (second parameter, list of YourClass
objects, List<YourClass>
)... and build a DataModel class around this collection ensuring the possibility of traversing trough the same number of elements in every row (even if the row does not have all the elements by just returning nulls or empty objects, or similar) by implementing the custom Iterator
.
精彩评论