title: Java Guava: Remove and put back elements from Multimap while iterating
body: <p>What I'm trying to achieve is the following: While iterating over both the keys and values of a <code>Multimap&lt;K,V&gt;</code>, I want to remove <strong>and put back</strong> elements from this list. Any approach I've tried ends up in a ConcurrentModificationException. This remove-and-put-back-approach is required to implement a backtrack search (see here: <a href="https://stackoverflow.com/questions/16508640/implementing-a-backtrack-search-with-heuristic">Implementing a backtrack search with heuristic?</a> ) </p>
  
  <p>This may look like:</p>
  
  <pre><code>Multimap&lt;K,V&gt; multimap = HashMultimap.create();
  Iterator keyIterator = multimap.keySet().iterator();
  
  while(keyIterator.hasNext()) {
    K key = keyIterator.next();
    Collection values = multimap.get(key);
  
    Iterator valueIterator = values.iterator();
    while(valueIterator.hasNext()) {
      V myValue = valueIterator.next();
      if(special) {
        valueIterator.remove();
        keyIterator.remove();
        // recursion
        // put back Collection with key but without myValue &lt;-- HOW?
      }
    }
  }
  </code></pre>