What I'm trying to achieve is the following: While iterating over both the keys and values of a Multimap<K,V>
, I want to remove and put back 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: Implementing a backtrack search with heuristic? )
This may look like:
Multimap<K,V> 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 <-- HOW?
}
}
}