Search code examples
javajtablehashmaptablemodellis

Java tablemodel hashmap vs list


I have a custom AbstractTableModel

That model stores the data in a HashMap. So for my method for getValueAt(int rowIndex, int columnIndex)

I do

new ArrayList<Object>(data.values()).get(index);

However my data has over 2000 entries, so doing this every single time whenever I have to get the data for my table creates a huge performance hit.

So what solution can you recommend?

Should I try using List to store all my data in instead of HashMap?
What is the accepted standard for storing data when using table models?

Thanks to anyone for their suggestion, and I aplogize for what might be a stupid question, but I am not too great when it comes to tables and how to store data in them.


Solution

  • A HashMap doesn't generally make a good fit for a table model because the table needs the ability to access data at an row/col location.

    A ArrayList of ArrayLists is a reasonable way to store a table model. This still gives you fast access. Getting to a particular row is a constant time lookup, and then getting the column is also a constant time lookup.

    If you don't want the overhead of the lists, you can always store the data in a 2D array.