I have code that creates and uses a collection such as:
List<Map<String, Object>> tableData;
This list of maps gets populated with n maps each representing one row in a database. Each row is represented as a map between the field name and the object corresponding to the field (the type is irrelevant in this case). Some of the fields might be missing. The number of fields, m is always much smaller than the number of rows (n ≈ 10000 × m). I need to reuse the same collection a few times to read through all the rows, so I can't just use some kind of lazy iterator.
Is there an efficient data structure to store this? Guava provides a Table
collection but that doesn't seem to fit the bill. I am thinking about creating an interface such as:
interface TableData{
int size();
Map<String, Object> get(int i);
// ... (interators, etc.)
}
And then create an implementation that uses one Map<String,List<Object>>
so that I only instantiate m lists instead of n maps and create maps on the fly only when needed but I was wondering if there was a more general purpose data structure out there.
Thanks
HashBasedTable
will not work for you ? Your in-memory table size is not super-big, and overhead of having HashMap vs ArrayList per row is a constant factor quite close to 3, according to stackoverflow.com/questions/1526596 Could you provide rough estimation of your memory constraints ? – Asdic