Groovy键集和值

Groovy键集和值,groovy,map,key,Groovy,Map,Key,有没有办法抓取一张地图的关键点并用它的值替换另一张地图的值 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

有没有办法抓取一张地图的关键点并用它的值替换另一张地图的值

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
}
所以基本上我想知道我是否能够根据地图中的值找到一个键,并用地图中键的值替换该值


有没有一种简单的方法可以做到这一点?

不知道可以这样直接分配它。Lol觉得有点傻,因为它太简单了。谢谢你的帮助,马修
if(pet.containsKey(wild.animal1))
{
    wild.animal1 = pet[wild.animal1];
}