title: Removing Map Entry from inner For loop
body: <p>I use a map in both outer and inner loop and I need to delete an entry in the map in the inner loop so that the deleted entry is not iterated in the outer or inner loop anymore.</p>
  
  <p>I tried remove() on the inner iterator but that does leads to an exception when iterating further on the outer loop.</p>
  
  <pre><code>Map&lt;String, String&gt; testMap = new HashMap&lt;&gt;();
  testMap.put("A", "AAA");
  testMap.put("B", "BBB");
  testMap.put("C", "CCC");
  testMap.put("D", "DDD");
  
  for(Iterator&lt;Map.Entry&lt;String, String&gt;&gt; it = testMap.entrySet().iterator(); it.hasNext();) {
      Map.Entry&lt;String, String&gt; outerEntry = it.next();
      for(Iterator&lt;Map.Entry&lt;String, String&gt;&gt; it1 = testMap.entrySet().iterator(); it1.hasNext();) {
          Map.Entry&lt;String, String&gt; innerEntry = it1.next();
          if(!outerEntry.getKey().equals(innerEntry.getKey()) &amp;&amp; !innerEntry.getKey().equals("D")) {
              // it1.remove();
              // remove entries "B" and "C" from testMap so that the next iteration in outer loop is "D"
              // also I don't require the entries "B" and "C" in the inner loop once they are deleted
          }
      }
  }
  </code></pre>
  
  <p>In the code given, the entries "B" and "C" must be deleted from the testMap on the first iteration of outer loop. The next iterator of outer loop should be of "D".</p>
  
  <p>Purpose of the code is to collect Map entries which have same values.</p>
  
  <pre><code>import java.util.List;
  import java.util.ArrayList;
  import java.util.Map;
  import java.util.HashMap;
  import java.util.Arrays;
  import java.util.Set;
  import java.util.HashSet;
  import java.util.stream.Collectors;
  
  public class Main {
  
      public static void main(String[] args)
      {
          Map&lt;String, Map&lt;String, List&lt;String&gt;&gt;&gt; resultMap = new HashMap&lt;&gt;();
  
          Map&lt;String, List&lt;String&gt;&gt; testMap = new HashMap&lt;&gt;();
          testMap.put("A", new ArrayList&lt;&gt;(Arrays.asList("AAA", "CCC", "BBB")));
          testMap.put("B", new ArrayList&lt;&gt;(Arrays.asList("AAA", "BBB", "CCC")));
          testMap.put("C", new ArrayList&lt;&gt;(Arrays.asList("CCC")));
          testMap.put("D", new ArrayList&lt;&gt;(Arrays.asList("DDD")));
          testMap.put("E", new ArrayList&lt;&gt;(Arrays.asList("DDD")));
          testMap.put("F", new ArrayList&lt;&gt;(Arrays.asList("AAA", "BBB", "CCC")));
  
          // process testMap in a way that resultMap contains
          // &lt;A, &lt;G1, [AAA, BBB, CCC]&gt;&gt;
          // &lt;B, &lt;G1, [AAA, BBB, CCC]&gt;&gt;
          // &lt;C, &lt;G2, [CCC]&gt;&gt;
          // &lt;D, &lt;G3, [DDD]&gt;&gt;
          // &lt;E, &lt;G3, [DDD]&gt;&gt;
          // &lt;F, &lt;G1, [AAA, BBB, CCC]&gt;&gt;
  
          // here G1, G2, G3 are groups that are created which represents testMap entries that have same values.
          // in resultMap, the order of [AAA, BBB, CCC] doesn't matter
  
          String gName = "G";
          int gId = 0;
          for(Map.Entry&lt;String, List&lt;String&gt;&gt; entry : testMap.entrySet()) {
              if (resultMap.containsKey(entry.getKey())) {
                  continue;
              }
              ++gId;
              String group = gName + String.valueOf(gId);
              Set&lt;String&gt; entryValuesSet = new HashSet&lt;&gt;(entry.getValue());
              Map&lt;String, List&lt;String&gt;&gt; groupEntries = new HashMap&lt;&gt;();
              groupEntries.put(group, entry.getValue());
  
              Set&lt;String&gt; groupSet = testMap.entrySet().stream().filter(e -&gt; !resultMap.containsKey(e.getKey()) &amp;&amp; new HashSet(e.getValue()).equals(entryValuesSet)).map(f -&gt; f.getKey()).collect(Collectors.toSet());
              // here is my problem.
              // Even though in the first iteration (entry "A") itself, entries "A", "B", "F" have assigned a group
              // they are still being checked on further iterations (entries "C", "D") and need a filter condition to exclude
              // which are really unnecessary checks if I could just delete those entries
  
              for (String g : groupSet) {
                  resultMap.put(g, groupEntries);
              }
          }
  
          System.out.println(resultMap);
      }
  }
  </code></pre>
  
  <p>Is there a way to delete the entries in testMap that have already assigned in a group so that the unwanted checks can be avoided?</p>