What is the best way to store a generic List in a Map value?

Viewed 58

I am doing a University assignment about developing a Dataframe library (we are supposed to import the data from files such as csv, json and txt). The data structure I chose is HashMap<String, ArrayList<Object>> where the key represents a column label and the value (List) is the column values storage. When the program reads a file the first row goes through a filter which determines which kind of Object it is (Integer, String...) in order to add the data in that way (list.add(new Integer(2)) for example) but I'm not sure if it is the best way to proceed because the program is supposed to make some operations as well such as sort values (comparator), query (predicate)... Take into account every column can contain a different data type.

I also thought about using ArrayList<String> instead and transforming the values when necessary but would not be too optimal and I prefer asking you first.

Thanks a lot in advance.

Example:

LatD LatM LatS NS LonD LonM LonS EW City State

41 5 59 N 80 39 0 W Youngstown OH

Assignment document: https://drive.google.com/file/d/1Saqiw23vQk7Yoh2csmuH2adcktNzDmjP/view?usp=sharing

1 Answers

I'd try using Guava's Multimap instead of constructing Map with List values yourself.

On the other hand I would think about the design. Is the number of columns always the same and you know upfront what values are there? If the answer is yes, how about creating your custom value object with named fields of concrete types? Something like this sample below and implement some Comparators etc if required.

public class ValueRow {
    private Integer LatD;
    ...
    private String City;
    private String State;
    ...
}
Related