2010-09-15 36 views
0

有沒有辦法抓住一張地圖的關鍵字並用其值替換另一張地圖的值?Groovy鍵值和值

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 
} 

所以基本上我想知道如果我能夠基於地圖野生值,並與地圖中的寵物鍵值來取代值來找到一把鑰匙。

有沒有簡單的方法來解決這個問題?

回答

2
if(pet.containsKey(wild.animal1)) 
{ 
    wild.animal1 = pet[wild.animal1]; 
} 
+0

不知道可以直接指定它。大聲笑有點愚蠢,它非常簡單。謝謝你的幫助馬修 – StartingGroovy 2010-09-16 19:17:22