我有以下地图:
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']]
然后我按城市分组地图:
def mapGrouped = map.groupBy { it.city }
我得到以下输出:
[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]]]
我希望能删除例如 [姓名:阿里,城市:香港]
我知道如何完成这项任务?
答案 0 :(得分:3)
由于地图已按城市分组,您可以按城市直接查找为关键字,并从表示为值的集合中删除所需的地图
def toRemove = [name:'Ali', city:'HK']
mapGrouped[toRemove.city] -= toRemove
println mapGrouped
当然,你可以确定你实际上正在寻找一个存在该群体的城市。