Friday, January 17, 2014

Delete values from a map through a loop

So you woke up today and you want to delete values from a map, as you loop through the map (For whatever reason). So you right this is groovy

Code
def m = [a:'aa', b:'bb']
def kset = m.keySet()
kset.each{
m.remove(it)
}
println m
Output
[a,b,c]
Wrong !!!!
You actually get a ConcurrentModificationException.... hahaha :D <- This is me laughing at you. So you need to
kset.each{ tempKset << it }
tempKset.each{
m.remove(it)
}

No comments:

Post a Comment