我想要实现的是:在迭代 a 的键和值时Multimap<K,V>
,我想从这个列表中删除并放回元素。我尝试过的任何方法都以 ConcurrentModificationException 告终。这种remove-and-put-back-approach 是实现回溯搜索所必需的(参见此处:Implementing a backtrack search with heuristic?)
这可能看起来像:
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?
}
}
}