views:

218

answers:

3

My TableModel implementations typically sit on an ArrayList to allow for efficient random access performance. However, ArrayList's remove(int) implementation looks fairly inefficient as it involves making a System.arrayCopy(...) call to shift all subsequent elements back by 1.

What approaches to people take to implementing TableModels? Is there a better data structure I should be considering? ... perhaps a 3rd party library?

Some more information: My table data can shrink and grow so any fixed-size buffer implementation isn't going to work.

Thanks in advance.

A: 

If you need to remove elements frequently you could choose a LinkedList implementation. You pay a bit of memory for speedy removals.

extraneon
Unfortunately this will significantly impact my access time as LinkedList is not random access, so this isn't really an option (esp. as my table is likely to contain ~10000 rows).
Adamski
+2  A: 

Your question reeks of "Premature Optimization".

On my computer, System.arrayCopy() can copy 1 million elements of data in roughly 13ms. So I suggest to measure whether this is really an issue. In the general case, ArrayList is faster and has a better memory performance than any other similar data structure.

Using a LinkedList would make all operations on the list (including remove()) slower since you will now have to traverse half of all list elements for each operation (on average). So most operations would go from O(1) to O(N/2).

Aaron Digulla
Thanks Aaron, that's a good point. TBH I was going to try ArrayList before building something bespoke anyway - I just wondered whether something more efficient existed out of the box.
Adamski
arrayCopy is highly optimized; you won't find anything that's faster than that. In your case, the copy will take 65ns on average, not something you'll ever notice.
Aaron Digulla
That is misleading. 13ms for doing it once is ok, but you definitely do not want to delete 10000 elements from that list. When you apply filters to that collection, that could easily be the case
Stephan Eggermont
When deleting many elements, copying the remaining elements into a new list is more efficient than deleting them individually. That is again faster than LinkedList. The only case where a LinkedList is more efficient if you can do the filtering in a single sweep ... but usually, you leave the original model alone, so you need the copy step anyway.
Aaron Digulla
+1  A: 

When implementing a TableModel you need to consider 2 things

1) creating and manipulating the model

2) rendering your table

The first is alot less important performance wise then the second.

You typicaly create your model once and do a tiny amount of manipulation, the rendering engine behind the table continuosly queries your tablemodel. This means that the retrieval of the information from a model has to be optimal, any action taken to speed up model manipulation at cost of model query is therefore to be avoided.

In my case i need both, heavy manipulation and fast rendering. So the choice i did was a sort of LinkedList implemenation for all manipulation combined with an array, both are kept synchronous , all inserting, manipulation deletion is being done through the linkedlist structure, all rendering with the array structure

Peter