0

I have a excel sheet from which i have to read all cells and store in 2D array,which i have already done.Now I want to store these 2D array elements in a hashmap. I tried using the following code but when i print the map it only prints the last read elements with its respective keys.It can be understood from the following example:-

1> My Excel sheet is:

enter image description here

2> Code to populate 2D array from excel sheet is:

String[][] excelData = new String[numRows][numCols];
            System.out.println("Populating Array....");
            for (int i=0; i<numRows; i++) 
            {
                HSSFRow row = sheet.getRow(i);
                for (int j=0; j<numCols; j++) 
                {
                    HSSFCell cell = row.getCell(j);
                    String cellValue = cellToString(cell); //cellToString is a function in class
                    excelData[i][j] = cellValue; //data stored in excelData[][] array
                }   
            }
            System.out.println("Array population complete");

3> My Code to convert 2D array into Hashmap

   System.out.println("putting in map");            
   HashMap map = new HashMap();
   for (int j = 0; j < numCols;j++) //Columns
   {                
    for (int i = 1; i < numRows;i++ ) //Rows
    {
     map.put(excelData[0][j], excelData[i][j]);
    }
   }
   System.out.println("Map values key :" +map);

4> Actual Output :-

Map values key : {Header 1=valueH1_3, Header 2=valueH2_3, Header 3=valueH3_3, Header 4=valueH4_3, Header 5=valueH5_3}

The above output is incorrect.

Expected Output:-

Map values key : {Header 1=valueH1_1,valueH1_2,valueH1_3, Header 2=valueH2_1,valueH2_2,valueH2_3, Header 3=valueH3_1,valueH3_2,valueH3_3, Header 4=valueH4_1,valueH4_2,valueH4_3, Header 5=valueH5_1,valueH5_2,valueH5_3}

Map is not able to retain previous iterated values of keys.Can anyone help please?

userkk
  • 23
  • 3
  • 1
    Add generic to your map. `String` class has overwritten `toString()` and `hashCode()` – catch23 Mar 11 '20 at 10:53
  • 1
    I'd recomment reading [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – OH GOD SPIDERS Mar 11 '20 at 10:55
  • Hello I'm not familiar with Java. As far as I know hashmap is a container that works similar to a dictionary, why don't you put the whole array value into the hashmap, but put each value in it? – Dang D. Khanh Mar 11 '20 at 11:00

1 Answers1

1
    HashMap map = new HashMap();
    for (int j = 0; j < numCols;j++) //Columns
    {
        for (int i = 1; i < numRows;i++ ) //Rows
        {
            //map.put(excelData[0][j], excelData[i][j]);
            if(map.containsKey(excelData[0][j])){
                String existingVal = (String) map.get(excelData[0][j]);
                String updatedVal = existingVal + "," + excelData[i][j];
                map.put(excelData[0][j], updatedVal);
            } else {
                map.put(excelData[0][j], excelData[i][j]);
            }
        }
    }
    System.out.println("Map values key :" +map);
Aditya Rewari
  • 2,343
  • 2
  • 23
  • 36
  • thanks for quick reply.The answer is coming something like this: Map values key {Header 1=[Ljava.lang.String;@735b5592, Header 2=[Ljava.lang.String;@735b5592, Header 3=[Ljava.lang.String;@735b5592, Header 4=[Ljava.lang.String;@735b5592, Header 5=[Ljava.lang.String;@735b5592} – userkk Mar 11 '20 at 10:58
  • @user10971281 do update if any miss.. I have tried this with sample data. Working good for me ! – Aditya Rewari Mar 11 '20 at 11:32
  • Yeah Its working perfectly for me.Thank you Aditya Rewari. Now i'll be getting into some more complex similar thing.If i'll be stuck in that then i'll post that too.thanks one again – userkk Mar 11 '20 at 11:37