Skip to content
Advertisement

Reading column from a CSV and save into a List<List> in java

I am trying to do a simple data frame project which can read, write and make changes from the imported CSV file. This is the CSV file content:

Name,Age,Salary
Lim,20,2000
Tan,20,3000
Mah,19,2500
Roger,10,4000

I’m trying to read the file and then export the data into columns and rows. And this is the code that I had write:

        String filePath = "...srcBook1.csv";
        String line;
        List<List<String>> COLUMNDATA = new ArrayList();
        List<List<String>> ROWDATA = new LinkedList();

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            while ((line = br.readLine()) != null) {
                List<String> column = Arrays.asList(line.split(","));
                COLUMNDATA.add(column);

            }

            int numCols = COLUMNDATA.get(0).size();
            List<String> rowData = new ArrayList();
            for(int j = 0; j<numCols; j++){
                rowData.clear();
                for(int z = 0; z<COLUMNDATA.size();z++){
                    String [] temp = COLUMNDATA.get(z).toArray(new String[0]);
                    String temp1 = temp[j];
                    rowData.add(temp1);
                }
               
                ROWDATA.add(rowData);
           
            }
        } catch (Exception e) {
            System.out.println(e);
        }

        System.out.println(COLUMNDATA.toString());
        System.out.println(ROWDATA.toString());

This is the output of the the code:

[[Name, Age, Salary], [Lim, 20, 2000], [Tan, 20, 3000], [Mah, 19, 2500], [Roger, 10, 4000]]
[[Salary, 2000, 3000, 2500, 4000], [Salary, 2000, 3000, 2500, 4000], [Salary, 2000, 3000, 2500, 4000]]

Can I ask why is the ROWDATA showing the duplicates?

Advertisement

Answer

The reason it doesn’t work it’s because the ROWDATA.add(rowData) ads the same reference to rowData every time, one solution is to clone the object:

    String filePath = "...srcBook1.csv";
    String line;
    List<List<String>> COLUMNDATA = new ArrayList();
    List<List<String>> ROWDATA = new LinkedList();

    try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
        while ((line = br.readLine()) != null) {
            List<String> column = Arrays.asList(line.split(","));
            COLUMNDATA.add(column);
        }
        int numCols = COLUMNDATA.get(0).size();
        ArrayList<String> rowData = new ArrayList<>();
        for(int j = 0; j < numCols; j++){
            rowData.clear();
            for(int z = 0; z < COLUMNDATA.size(); z++){
                String[] temp = COLUMNDATA.get(z).toArray(new String[0]);
                String temp1 = temp[j];
                rowData.add(temp1);
            }
            ROWDATA.add((ArrayList<String>) rowData.clone());
        }
    } catch (Exception e) {
        System.out.println(e);
    }

    System.out.println(COLUMNDATA.toString());
    System.out.println(ROWDATA.toString());

Outputs:

[[Name, Age, Salary], [Lim, 20, 2000], [Tan, 20, 3000], [Mah, 19, 2500], [Roger, 10, 4000]]
[[Name, Lim, Tan, Mah, Roger], [Age, 20, 20, 19, 10], [Salary, 2000, 3000, 2500, 4000]]
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement