5

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?

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

My only concern is that it uses Vectors for the storage which will add unnecessary synchronization overhead if you really are accessing rows and columns thousands of times per second. The approach is valid though, just maintain a List of Lists.
Its parent,AbstractTableModel, makes it easy to use your own data structure. You'll need to code your own updates, but the notification mechanism is in place. java.sun.com/javase/6/docs/api/javax/swing/table/…
1

If the performance is critical, you can use a 2D-array, and implement a reallocation algorithm (e.g. doubling) so that it can grow.

1 Comment

Though about it. Will do that if the first proposed solution's performance proves not to be enough.
1

HashBasedTable class from Google Guava libraries does this. There is also TreeBasedTable if rows need to be in sorted order.

Comments

0

Perhaps JQL or HSQL DB

2 Comments

JQL is just a syntactic sugar for querying existing data structures. And I really doubt that full-blown SQL database is a solution in my case (I need to select rows/columns from that table thousands times per seconds).
HSQLDB is very lightweight, I think it's worth at least to run a performance test to see if it answers your requirements.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.