将变量添加到lazyMap Reference

时间:2016-10-19 17:56:45

标签: groovy

我希望将String传递给一个函数,该函数允许我在LazyMap

中动态更改我要查找的密钥
public analyzeLazyMap(String valueToCheck, String keyToAnalyze, ArrayList<LazyMap> arrayOfLazyMaps){
    for (LazyMap myLazyMap in arrayOfLazyMaps) {
        if (valueToCheck == myLazyMap.$keyToAnalyze) {
             return 'bar'
        }
    }
}

所以在这个电话中我希望myKey进入“Do Something”部分“。假设foobar将在myArrayListofLazyMaps

String foo = analyzeLazyMap('foobar', 'myKey', myArrayListofLazyMaps,)
println(foo)
//Console Output == bar

2 个答案:

答案 0 :(得分:0)

好的,想通了。我的数据不正确

 public analyzeLazyMap(String valueToCheck, String keyToAnalyze, ArrayList<LazyMap> arrayOfLazyMaps){
     for (LazyMap myLazyMap in arrayOfLazyMaps) {
         if (valueToCheck == myLazyMap."$keyToAnalyze") {
              return 'bar'
         }
     }
 }

答案 1 :(得分:0)

一点语法糖

arrayOfLazyMaps.each {lazyMap ->
    if (lazyMap."$keyToAnalyze" == valueToCheck)
        return 'bar'
}