0

I am trying to read an excel sheet and save it in Java Data Structure List> list Basically I am having a table in the excel sheet

|Name|FirstName|emp_ID|Age|
|aaaa|bbbbbbbbb|111111|40 |
|cccc|fffffffff|222222|25 |

where the keys will be

|Name|FirstName|emp_ID|Age|

my list of map should look as bellow

{Name=aaaa, FirstName=bbbbbbbbb, emp_ID=111111, Age=40}
{Name=cccc, FirstName=fffffffff, emp_ID=222222, Age=25}

but my list is storing the second map two times

{Name=cccc, FirstName=fffffffff, emp_ID=222222, Age=25}
{Name=cccc, FirstName=fffffffff, emp_ID=222222, Age=25}

any idea how to fix the issue please or any better suggestion
Thank You in advance

Here is the code That I wrote

        workbook = WorkbookFactory.create(inStream);
    workSheet = workbook.getSheetAt(0);

    DataFormatter df = new DataFormatter();
    Map<String, String> myMap = new LinkedHashMap<>();
    List<Map<String, String>> list = new ArrayList<>();
    row = workSheet.getRow(0);

    ArrayList<String> headersName = new ArrayList<String>();

    for (int j = 0; j <= row.getPhysicalNumberOfCells(); j++) {
        row.getCell(j);
        if ((df.formatCellValue(row.getCell(j)).isEmpty())) {
            continue;
        } else {
            headersName.add(df.formatCellValue(row.getCell(j)));
        }
    }

    System.out.println(headersName);
    OUTER: for (Row myrow : workSheet) {
        for (int i = 0; i < myrow.getLastCellNum(); i++) {
            if (myrow.getRowNum() == 0) {
                continue OUTER;
            }   
            String value = df.formatCellValue(myrow.getCell(i));
            myMap.put(headersName.get(i), value);
        }
        list.add(myMap);
    }

    System.out.println(list.size());
    for (Map<String, String> map : list) {
        System.out.println(map);
    }

the print of my list {Name=cccc, FirstName=fffffffff, emp_ID=222222, Age=25} {Name=cccc, FirstName=fffffffff, emp_ID=222222, Age=25}

cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31
A A
  • 1
  • 1
  • I would not trying to press all in collections. I would doing this using a kind of table model. A `ExcelTableModel` similar to the [javax.swing.table.DefaultTableModel](https://docs.oracle.com/javase/8/docs/api/javax/swing/table/DefaultTableModel.html). See https://stackoverflow.com/questions/50079739/how-to-write-dynamic-excel-file-number-of-column-can-exceed-into-java-objects/50083656#50083656. – Axel Richter Jan 12 '19 at 06:47

1 Answers1

0

The issue lies in myMap.put(headersName.get(i), value). In that line, if the key already exists in the map, it overrides it. But later on, list.add(myMap) passes a reference to the same map, not a new version of it. So on the second iteration, when the line overrides the value, it overrides it on the first map already in the list. See below for a basic example of the same issue.

import java.util.*;

public class Example {
     public static void main(String []args) {
        Map<String, String> myMap = new LinkedHashMap<>();
        List<Map<String, String>> list = new ArrayList<>();
        myMap.put("abc", "123");
        list.add(myMap);
        System.out.println(list.get(0));
        myMap.put("abc", "234");
        System.out.println(myMap);
        System.out.println(list.get(0));
     }
}

To fix it, look into deep copies so that changing one doesn't change them all.

Calvin Godfrey
  • 2,171
  • 1
  • 11
  • 27