The idiomatic way to iterate over Koloboke collection with modifications (key removals and updates, but not additions) is via cursor
:
for (LongIntCursor cur = map.cursor(); cur.moveNext();) {
long key = cur.key();
int value = cur.value();
if (checkSomething(key, value)) {
cur.remove(); // remove the entry
} else {
cur.setValue(newValue); // update the value
}
}
Additions are not supported, it should throw ConcurrentModificationException
, the same way as java.util.HashMap
do. The reason why this is so - if addition triggers full map rehash, it's impossible to finish iteration properly.
As a workaround, you could collect entries you want to insert into the map during iteration, and perform a bulk insert after the iteration:
// You could get primitive lists from fastutil, gs, hppc or trove
LongList keysToPut = keysToPutThreadLocal.get();
keysToPut.clear();
IntList valuesToPut = valuesToPutThreadLocal.get();
valuesToPut.clear();
for (LongIntCursor cur = map.cursor(); cur.moveNext();) {
long key = cur.key();
int value = cur.value();
if (checkSomething(key, value)) {
cur.remove(); // remove the entry
} else {
// want to insert a <newKey, newValue> entry
keysToPut.add(newKey);
valuesToPut.add(newValue);
}
}
// bulk insert
for (int i = 0, toPut = keysToPut.size(); i < toPut; i++) {
map.put(keysToPut.get(i), valuesToPut.get(i));
}