title: ConcurrentModificationException in java while deleting entries from nested hashmap
body: <pre><code>import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.stream.IntStream;
  
  
  public class HelloWorld{
  
       public static void main(String[] args) {
          Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
  
          IntStream.range(0, 20).forEach(i -&gt; map.put(Integer.toString(i), i % 2 == 0 ? null : "ok"));
  
          for (Map.Entry&lt;String, String&gt; entry : map.entrySet()) {
              if (entry.getValue() == null) {
                  map.remove(entry.getKey());
              }
          }
      }
  }
  </code></pre>
  
  <p>This is a sample code where I am trying to delete the null values off the given Hashmap. But this code is giving ConcurrentModificationException. Any idea how to fix this?</p>
  
  <p>EDIT: Thanks to YCF_L, the above code helps if I replace the whole loop with the <code>map.entrySet().removeIf(entity -&gt; entity.getValue() == null);</code></p>
  
  <p>Problem 2: </p>
  
  <p>What if the hashmap is nested?</p>
  
  <ul>
  <li>Case 1 -> I want to delete if value is null</li>
  <li>Case 2 -> I want to delete if the value is a hashmap whose each element in the nested hash is null and so on if its nested nested.</li>
  </ul>
  
  <p><strong>Ex code:</strong> </p>
  
  <pre><code>public static void removeEmptyValues(Map&lt;String, Object&gt; entityMap) {
      for (Map.Entry&lt;String, Object&gt; entry : entityMap.entrySet()) {
          String key = entry.getKey();
          Object value = entry.getValue();
          if (value == null) {
              entityMap.remove(key);
          } else if (value instanceof Map) {
              removeEmptyValues((Map) value);
              if (((Map) value).isEmpty()) {
                  entityMap.remove(key);
              }
          }
      }
  }
  </code></pre>