Question

I have a CSV file that I'd like to store as a Java object. I would like the name of the columns to be the first dimension of the array, and key pair values the second dimension of the array. I've tried different solutions (mostly LinkedHashMaps) but none seem to work properly.

The CSV looks like this:

TimeStamp;Column 1;Column 2;Column3
1385733406;Value1;Value12;Value13
1385733409;Value21;Value22;Value23
1385733411;Value31;Value32;Value33

I would like the array to look something like this:

["Column 1"]
    ["1385733406","Value1"]
    ["1385733409","Value21"]
    ["1385733411","Value31"]
["Column 2"]
    ["1385733406","Value2"]
    ["1385733409","Value22"]
    ["1385733411","Value32"]
["Column 3"]
    ["1385733406","Value2"]
    ["1385733409","Value22"]
    ["1385733411","Value33"]

This way, I would be able to query the object and retrieve all the key pair values from a given column, for instance, all the data from Column 1. Using HashMaps doesn't seem to work because they require two arguments, and doing this doesn't seem to be the proper way. This is the code I could come up with so far, which I don't think is the right track but it's all I could come up with. I'm using OpenJDK 1.7

public class CsvCollection {
    public Map<String,Map<String,Integer>> Results = new LinkedHashMap<String, Map<String,Integer>>();

    public CsvCollection(){

    }

    public void parseCsvResultFile(File csvFile){       
        CSVReader reader = null;
        List myEntries = null;
        try {
            reader = new CSVReader(new FileReader(csvFile.getAbsolutePath()), ';');
        } catch (FileNotFoundException e) {
            System.out.println("Error opening [], aborting parsing");
        }
        try {
            myEntries = reader.readAll();
        } catch (IOException e) {
            System.out.println("Error reading content of CSV file (but file was opened)");
        }

        for(String header: (String[]) myEntries.get(0)){
            Results.put(header, null);
            // What now?
        }       
    }
}
Was it helpful?

Solution

You can make 2 change as follows to implements the function you needed.

1) Change the following code

public Map<String,Map<String,Integer>> Results = new LinkedHashMap<String, Map<String,Integer>>();

to

public Map<String, List<String[]>> Results = new LinkedHashMap<String, List<String[]>>();

This change is made because for a specify column, like Column 1, it has 3 rows with Timestamp and corresponding Column value. You need to use a List to store them.

2) Change the following for-loop

   for(String header: (String[]) myEntries.get(0)){
        Results.put(header, null);
        // What now?
    } 

to

    String[] headerColumns = (String[]) myEntries.get(0);
    // First column is TimeStamp, skip it
    for (int i = 1; i < headerColumns.length; i++) {

        List<String[]> list = new ArrayList<String[]>();
        for (int rowIndex = 1; rowIndex < myEntries.size(); rowIndex++) {
            String[] row = (String[]) myEntries.get(rowIndex);
            list.add(new String[] { row[0], row[i] });
        }

        Results.put(headerColumns[i], list);
    }

With the above 2 changes, if you print the Results (type of Map< String, List< String[] > >) in console using the following code,

   for(Map.Entry<String, List<String[]>> entry : Results.entrySet())
    {
        System.out.printf("[%s]\n",entry.getKey());
        for(String[] array : entry.getValue())
        {
            System.out.println(Arrays.toString(array));
        }
    }

you will get the result you need:

[Column 1]
[1385733406, Value1]
[1385733409, Value21]
[1385733411, Value31]
[Column 2]
[1385733406, Value12]
[1385733409, Value22]
[1385733411, Value32]
[Column3]
[1385733406, Value13]
[1385733409, Value23]
[1385733411, Value33]

Note: the above example is executed using the content from a CSV file below:

TimeStamp;Column 1;Column 2;Column3
1385733406;Value1;Value12;Value13
1385733409;Value21;Value22;Value23
1385733411;Value31;Value32;Value33
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top