JAVA apache-poi: Matching strings in iteration, need to write final values into specific rows
<p>I'm working on a code that reads excel files (looking at cells in columns) and then writes something that looks like the following tables:</p>

<p>I have an excel file that looks like this:</p>

<pre><code>      col1     col2    col3    col4
      -----------------------------
row1 | 2,3,1    _        1      w
row2 | 3,2,7    _        2      x
row3 |   _      _        3      y
row4 |  4,9     _        4      z
</code></pre>

<p>I'm writing some values (using Apache POI) in column 2 that look like this:</p>

<pre><code>      col1     col2    col3    col4
      -----------------------------
row1 | 2,3,1  x,y,w      1      w
row2 | 3,2,7   y,x       2      x
row3 |   _      _        3      y
row4 |  4,9     z        4      z
</code></pre>

<p>Essentially, column 3 and column 1 are being compared, and if cell (1,1) has values matched in column 3, column 4's values (which correspond to column 3) are written into column 2. </p>

<p>Here's my code: Important areas are hashed out.</p>

<pre><code>for (int rowIndex = 0; rowIndex &lt; 50000; rowIndex++) {          
for (int columnIndex = 0; columnIndex &lt; 30; columnIndex++) {
                try {
                    XSSFCell COL0 = worksheet.getRow(rowIndex).getCell(0);
                    String buddy = COL0.getStringCellValue();
                    String[] newton = buddy.split(",");
                    XSSFCell COL1 = worksheet.getRow(rowIndex).getCell(1);
                    String james = COL1.getStringCellValue();
                    XSSFCell COL2 = worksheet.getRow(rowIndex).getCell(2);
                    String path1 = COL2.getStringCellValue();

                     //IMPORTANT AREA
                    for (String i : newton) {
                        if (i == james) { 
                        System.out.println(path1); //How do I get this (column 3) to only pull out the cells that correlate to col2 and col1 matches.

                    } else {
                        System.out.println(" ");
                    }} 
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
</code></pre>

<p>Really new to Java, I'm just confused as to the specific syntax for this problem. Looked through the documentation, but I've been stuck for a while on exactly what I should do. Any advice would really help out. Thank you!</p>

<p><strong>Edit: 2 sets of columns:</strong></p>

<p><a href="https://i.stack.imgur.com/YWzX1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YWzX1.png" alt="http://i.imgur.com/dhVSYPH.png"></a></p>

<pre><code>//del if necessary - maps keys from col1 to col4 - MAIN CHANGES
public static HashMap&lt;String, String&gt; getColusmnMap(Sheet sheet, int col1, int col4) {
       HashMap&lt;String, String&gt; hashMasp = new HashMap&lt;String, String&gt;();
       for (Row rowie : sheet) {
        String koop = getStringCellValue(rowie.getCell(col1));
        String valeue = getStringCellValue(rowie.getCell(col4));
        if (hashMasp.containsKey(koop)) {
         valeue = hashMasp.get(koop) + valeue;
        }
        hashMasp.put(koop, valeue);
       }
       return hashMasp;
     }







  //del if necessary - Col4 and 5 hashmap - MAIN CHANGES
   HashMap&lt;String, String&gt; columnMapio = getColumnMap(sheet, 4, 5);

   for (Row rowie : sheet) {
    String cell0Valeue = getStringCellValue(rowie.getCell(0));
    String[] koops = cell0Valeue.split(",");
    String cell1Values = "";
    for (String koop : koops) {
     if (columnMapio.containsKey(koop)) {
      cell1Values += columnMapio.get(koop) + ",";
     }
    }
    if (cell1Values.length()&gt;0) cell1Values = cell1Values.substring(0, cell1Values.length() -1);
    System.out.println(cell1Values);
    Cell cell = rowie.getCell(1);
    if (cell != null) {
      cell.setCellValue(cell1Values);
    } else {
      rowie.createCell(1).setCellValue(cell1Values);
    }
   }
</code></pre>