0
votes

I have the following map:

def map = [[name:'Clark', city:'London'], [name:'Sharma', city:'London'], [name:'Maradona', city:'LA'], [name:'Zhang', city:'HK'],[name:'Ali', city: 'HK'], [name:'Liu', city:'HK']]

Then I grouped the map by city:

def mapGrouped = map.groupBy { it.city }

And I get the following output:

[London:[[name:Clark, city:London], [name:Sharma, city:London]], LA:[[name:Maradona, city:LA]], HK:[[name:Zhang, city:HK], [name:Ali, city:HK], [name:Liu, city:HK]]]

Here I want to be able to delete for example [name:Ali, city:HK]

Any idea how can I complete this task?

1

1 Answers

3
votes

Since map is grouped by city already, you can directly lookup by city as key and remove the desired map from the collection represented as value

def toRemove = [name:'Ali', city:'HK']
mapGrouped[toRemove.city] -= toRemove

println mapGrouped

Of course you would have made sure you are actually looking up a city that exists the group.