Consider a huge CSV with the following structure (modified for simplicity):
ID, NAME, ADDRESS, PHONE, MAIL
1, Jon, UK, 403, jon@skeet.com
2, Marc, UK, 292, marc@gravel.com
3, Darin, France, 291, darin@dimitrov.com
...
(Some million records)
The natural data structure for quick fetch is a Hash Table, where every ID is a key and NAME, ADDRESS, PHONE, MAIL are the value. My dillema is the data structure of the values.
Storing it in a HashMap where every row header is the key is a waste of space, because the row headers are exactly the same for each row. Storing it as an Array would lose the metadata for each item, because the reader
I was thinking of two approaches:
-
Overload Java’s Hashmap. The row headers will be stored once, and every
IDwill be associated with a String Array. Theget()method will be overloaded so that it will return a map between the header rows and the corresponding fields in the row. -
Create a dumb class which stores the data for each row using getters and setters (
row.getMail(),row.getAddress(),…)
What’s the right way to go, in terms of memory efficiency, type safety and speed?
I’d go for the “dumb” class instead of overloading a collection.
I don’t know about type safety or speed, but I would say your code will be more readable. Those values go together; encapsulate them in an object to emphasize the point. Is there any behavior associated with them besides get/set? If yes, then so much the better.