You cannot remove an element from a collection while iterating it unless you use an Iterator
.
This is what's causing the exception.
buyingItemEnumerationMap.remove(item.getKey());
Use Iterator#remove() to remove an element while iterating over your collection like
Iterator> iterator =
buyingItemEnumerationMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry item = iterator.next();
if(RandomEngine.boolChance(50)){ //will delete?
iterator.remove();
}
//..
}
EDIT : (in response to OP's comment)
Yes, the deletions done through Iterator#remove()
over the Set
returned by HashMap.entrySet() would reflect in the underlying Map
as the Set
is backed by it. Quoting the JavaDoc here:
Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.