【问题标题】:Groovy keyset and valuesGroovy 键集和值
【发布时间】:2010-09-15 01:45:02
【问题描述】:

有没有办法获取一张地图的键并用它的值替换另一张地图的值?

def wild = [animal1:"pet3", animal2:"dog", animal3:"pig"]
def pet = [pet1:"hamster", pet2:"fish", pet3:"cat"]

if(pet.containsKey(wild.animal1)) {
    //replace wild.animal1 with the value contained in pet3 for example
    //so wild.animal1 would equal "cat"
} else {
    //dont change value
}

所以基本上我想知道我是否能够根据地图中的值找到一个键,并将该值替换为地图宠物中键的值。

有没有简单的方法来解决这个问题?

【问题讨论】:

    标签: groovy map key


    【解决方案1】:
    if(pet.containsKey(wild.animal1))
    {
        wild.animal1 = pet[wild.animal1];
    }
    

    【讨论】:

    • 不知道可以这样直接分配它。大声笑感觉有点愚蠢,它是如此简单。谢谢你的帮助马修
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2014-05-13
    • 1970-01-01
    • 2018-01-15
    • 2017-09-27
    相关资源
    最近更新 更多