正面案例:可以进入列表
groovy> println GroovySystem.version
groovy> final data1 = [[99,2] , [100,4]]
groovy> data1.collect{x,y->x+y}
2.2.1
Result: [101, 104]
否定案例:不能做同样的
groovy> println GroovySystem.version
groovy> final data = [x:[99,2] , y:[100,4]]
groovy> data.collect{key, val->
groovy> val.collect{x,y->x+y}
groovy> }.flatten()
2.2.1
Exception thrown
groovy.lang.MissingMethodException: No signature of method: ConsoleScript80$_run_closure1_closure2.doCall() is applicable for argument types: (java.lang.Integer) values: [99]
Possible solutions: doCall(java.lang.Object, java.lang.Object), findAll(), findAll(), isCase(java.lang.Object), isCase(java.lang.Object)
at ConsoleScript80$_run_closure1.doCall(ConsoleScript80:5)
at ConsoleScript80.run(ConsoleScript80:4)
答案 0 :(得分:2)
也许你想要
data.values().collect{x,y->x+y}