Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT:</p> <p>I hadn't noticed that you weren't really <em>adding</em> to the map - you were just changing the value within the entry. In this case, pstanton's (pre-edit<sup>1</sup>) solution is <em>nearly</em> right, but you should call <code>setValue</code> on the entry returned by the iterator, rather than calling <code>map.put</code>. (It's <em>possible</em> that <code>map.put</code> will work, but I don't believe it's guaranteed - whereas the docs state that <code>entry.setValue</code> <em>will</em> work.)</p> <pre><code>for (Iterator&lt;Map.Entry&lt;String, Node&gt;&gt; it = map.entrySet().iterator(); it.hasNext();) { Map.Entry&lt;String, Node&gt; entry = it.next(); Node n = entry.getValue().optimize(); if(n == null) { it.remove(); } else { entry.setValue(n); } } </code></pre> <p>(It's a shame that <code>entry</code> doesn't have a <code>remove</code> method, otherwise you could still use the enhanced for loop syntax, making it somewhat less clunky.)</p> <p><strong>Old answer</strong></p> <p>(I've left this here for the more general case where you just want to make arbitrary modifications.)</p> <p>No - you should neither add to the map nor remove from it directly. The set returned by <code>HashSet.keySet()</code> is a view onto the keys, not a snapshot.</p> <p>You <em>can</em> remove via the iterator, although that requires that you use the iterator explicitly instead of via an enhanced for loop.</p> <p>One simple option is to create a new set from the original:</p> <pre><code>for (String key : new HashSet&lt;String&gt;(map.keySet())) { ... } </code></pre> <p>At this point you're fine, because you're not making any changes to the set.</p> <p>EDIT: Yes, you can definitely remove elements via the key set iterator. From the docs for <code>HashMap.keySet()</code>:</p> <blockquote> <p>The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.</p> </blockquote> <p>This is even specified within the <code>Map</code> interface itself.</p> <hr> <p><sup>1</sup> I decided to edit my answer rather than just commenting on psanton's, as I figured the extra information I'd got for similar-but-distinct situations was sufficiently useful to merit this answer staying.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload