I create a map like this:
def myMap = [:]
A map is basically an object for a key and an int for a value. When I iterate over the map, I decrease the value, and if it is 0, I delete it. I already tried myMap.remove()
, but I got a ConcurrentModificationError
- this is fair enough. So I move on to using it.remove()
, which gives me weird results.
Basically, my code is this:
myMap.each { it.value--; if( it.value <= 0 ) it.remove(); }
Simple enough. My problem is that if I print myMap.size()
before and after removal, they are the same. If I call myMap.containsKey( key )
, it gives me true
, the key is still there.
But, if I print the card as follows:
myMap.each { System.out.println( "$it.key: $it.value" ); }
I get nothing, and calling myMap.keySet()
and myMap.values()
returns empty.
Does anyone know what is going on?
source share