title: Removing Values From a Map in a loop
body: <p>I am trying to compare two maps and remove values from one map that are contained in a second map. Here is the code: </p>
  
  <pre><code>    HashMap&lt;String, String&gt; firstMap = new HashMap&lt;&gt;();
      HashMap&lt;String, String&gt; secondMap = new HashMap&lt;&gt;();
  
      firstMap.put("keyOne", "valueOne");
      firstMap.put("keyTwo", "valueTwo");
      firstMap.put("THIS KEY WILL BE REMOVED", "valueThree");
  
      System.out.println("\nMAP ONE\n" + firstMap +  "\n");
  
      secondMap.put("keyOne", "valueOne");
      secondMap.put("keyTwo", "valueTwo");
  
      System.out.println("\nMAP TWO\n" + secondMap +  "\n");
  
      Iterator&lt;String&gt; firstMapIterator = firstMap.keySet().iterator();
  
      if(!firstMap.equals(secondMap)){
  
              firstMapIterator.next();
  
              for(String key : firstMap.keySet()){
  
                  if(firstMap.containsKey(key) &amp;&amp; !secondMap.containsKey(key)){
                      firstMapIterator.remove();
                      break;
                  }
              }
      }
      System.out.println("\nMAP ONE MATCHING MAP TWO?\n" + firstMap +  "\n");
  </code></pre>
  
  <p>Now, the code does remove an element from the map but not the one I was expecting. As you can see in the code in <code>firstMap</code> I have entered a third key of which is the one I expect to be removed. However, this is the final contents of <code>firstMap</code>I seem to be getting. </p>
  
  <pre><code>{keyOne=valueOne, THIS KEY WILL BE REMOVED=valueThree}
  </code></pre>
  
  <p>Any ideas? Thanks</p>
  
  <p>Edit: The goal of this code is to: 
   - Compare two maps
   - Increment through each key
   - Remove key from firstMap if it is not found in secondMap</p>