Reputation: 2684
I need a Java implementation of table-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?
Upvotes: 5
Views: 3210
Reputation: 121
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
.
Upvotes: 0
Reputation: 27677
HashBasedTable class from Google Guava libraries does this. There is also TreeBasedTable if rows need to be in sorted order.
Upvotes: 1
Reputation: 6411
If the performance is critical, you can use a 2D-array, and implement a reallocation algorithm (e.g. doubling) so that it can grow.
Upvotes: 1
Reputation: 324108
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.
Upvotes: 3