0

I have to compare two Excel files (with different data) and create two new Excel tables:

  1. Table 1 contains all matching entries
  2. Table 2 contains all entries that not match

Therefore I iterate over both Excel files and store the matching entries in a LinkedHashMap. In a second LinkedHashMap I store all entries from the Excel file. With this two Maps I want to identify the delta.

To identify the delta I compare both lists and now want to remove all entries from the complete list, if the entry is already in the list with the matching ones.

I tried different solutions - all with the result that the code is running but never an entry is really removed. Can anyone help please?

Heres my code:

// This code fills both Maps
LinkedHashMap<String, String>  liste_matches = new LinkedHashMap<String, String> ();    
LinkedHashMap<String, String>  liste_complete = new LinkedHashMap<String, String> ();   

while(worksheet1.getLastRowNum() >= j){
    liste_complete.put(String.valueOf(worksheet1.getRow(j).getCell(18)), "");

    // Counter for loop, loops trough Telekom datasets
    int i = 1;

    while(worksheet2.getLastRowNum() >= i)
    {       
        if(String.valueOf(worksheet1.getRow(j).getCell(18)).equals(String.valueOf(worksheet2.getRow(i).getCell(9))))
        {
            if(!liste_matches.containsKey(String.valueOf(worksheet1.getRow(j).getCell(18)))){
                liste_matches.put(String.valueOf(worksheet1.getRow(j).getCell(18)), "");
            }
        }
    }

    // build Excel table
}

This is my code I used to compare both lists and remove all entries from liste_complete that are already in liste_matches.

I first tried this (I inserted the ArrayList for my second try...). It's running but without any effect to the list. ArrayList list = new ArrayList();

for(Map.Entry<String,String> keyDelta : liste_complete.entrySet())
{
    for(Map.Entry<String,String> key : liste_matches.entrySet()){
        if(keyDelta.equals(key)){
            liste_complete.remove(keyDelta);
            list.add(entry.getValue());
        }
    }   
}

Afterwards I tried this but also without any effect to the List: for(int c = 0; c < list.size(); c++) { String str = list.get(c); liste_complete.remove(str); }

I found this solution in StackOverflow, but that returns java.lang.IllegalStateException

Iterator<Map.Entry<String,String>> iter = liste_complete.entrySet().iterator();
while (iter.hasNext()) {
    Map.Entry<String,String> entry = iter.next();
    for(Map.Entry<String,String> key : liste_matches.entrySet()){
        if(key.getValue().equalsIgnoreCase(entry.getValue())){
            iter.remove();

        }               
    }   
}
A. Twitty
  • 1
  • 1
  • Have you tried using a debugger; or at least adding trace statements to actually understand the data that is pulled into your lists? You see, you put up quite some complex code, but without any input data; it is very hard to debug what is going on. – GhostCat Jan 11 '17 at 14:51
  • 1
    Suggestion: 1. Isolate your core code so that it has no dependency to the Excel stuff. 2. Create some simple test data example. 3. Write a JUnit test that fails demonstrating your problem. – Gustave Jan 11 '17 at 14:52

2 Answers2

0

AFAIK you can't remove element from a list you're iterating on. I suggest you 2 solutions:

  1. iterate on your lists to check for matching keys and store the match in the third list; then iterate on the third list and remove from liste_complete
  2. refactor the first piece of code of your question so that you store in one list the matching values and in the other the non-matching. Pseudo code could be:

    for worksheet1 row
      for worksheet2 row
          if(match)
             liste_matches.add(...)
          else
             liste_non_matches.add(...)
    

    In this way you do not have to remove elements afterwards.

Marco Stramezzi
  • 2,143
  • 4
  • 17
  • 37
0

Thanks a lot for your hints.

I already debugged the code but didn't understand the problem - I think it really was a problem of the complex input data.

I did not compare the keys via key.getKey() but only with key and that seems to cause problems in the comparison. Anyway, my code runs with this snippet:

            for(Map.Entry<String,String> keyDelta : liste_Complete.entrySet()){

            if(!liste.containsKey(keyDelta.getKey())){
                delta_liste.put(String.valueOf(keyDelta), "");
            }
        }
A. Twitty
  • 1
  • 1