如何删除Groovy映射中的值

时间:2018-06-12 21:10:48

标签: groovy

说我有这张地图

def map = [:]
map.put("us-east-1", ["vol-66c16ec2", "vol-654ce2c1", "vol-01234567"])
map.put("us-east-2", ["vol-12345678", "vol-87654321", "vol-abcdefgh"])

导致......

{
  "us-east-1" : [
    "vol-66c16ec2", "vol-654ce2c1", "vol-01234567"
  ],
  "us-east-2" : [
    "vol-12345678", "vol-87654321", "vol-abcdefgh"
  ]
}

如何迭代地图,以便当value =“vol-abcdefgh”时,我想删除该条目。所以伪代码就是......

for (it=iterate_through_map) {
  if (it == "vol-abcdefgh") {
     remove_entry(it)
  }
}

现在生成的地图......

{
  "us-east-1" : [
    "vol-66c16ec2", "vol-654ce2c1", "vol-01234567"
  ],
  "us-east-2" : [
    "vol-12345678", "vol-87654321"
  ]
}

2 个答案:

答案 0 :(得分:4)

请考虑以下内容(编辑,通过dmahapatro优化改进更新):

def target = 'vol-abcdefgh'

def map2 = map.collectEntries { k, v -> [k, v - target] }

我已经确认它在Jenkins管道中工作(与扩展运算符*不同)。

答案 1 :(得分:0)

直接前进就像是

map.values()*.removeAll{ 'vol-abcdefgh' == it }

有关removeAll()

的详细信息,请参阅docs