2016-07-27 23 views
0

解決鑰匙我很奇怪,爲什麼當它從一個字符串構造和可變def theKey="command$i"在常規地圖

def workingDir1 = "my/path" 
def command1 = "command1" 

def i=1 
def theKey="command$i" 

Map<String,List> map1 = new HashMap<String,String>(); 
map1.put("command1", workingDir1); 


def value = map1.get(theKey) 
println "$theKey $value" 

value = map1.get(command1) 
println "$command1 $value" 

輸出的關鍵解析爲空值:

command1 null 
command1 my/path 

是否有如何讓這個工作?

回答

1

這裏的問題是不同的類。 def theKey="command$i"創建一個GStringmap1.put("command1", workingDir1);使用String作爲密鑰。

爲了讓你的價值了使用theKey你要做的:

map1.get(theKey.toString()) 
+0

也許不是一個錯誤,但我認爲這是一個缺點,即語言的建築師應該考慮 - 作爲地圖使用時把GString的成字符串鍵。思考? – mohsenmadi

+0

它在文檔 –

+0

中特別提到,謝謝doelleri這樣做!我沒有意識到它們是不同的字符串類型。 –