[英]Groovy each closure error
I know Java but completely new to Groovy. 我知道Java,但对Groovy来说是全新的。 I have got some legacy code in Groovy to work on.
我在Groovy中有一些遗留代码可以处理。
I have below method in Groovy: 我在Groovy中有以下方法:
def mapMyNotificationsByFruits(prefs,fruits) {
def map = [:]
prefs.each { MainNotification cn ->
cn.fruits.each {
MyNotification an ->
def ex = map[(an.fruitId)]
if (!ex) ex = []
ex.add(an)
map[(an.fruitId)] = ex
}
}
log.info("map is: $map")
return map
}
Above method gets called from another method as below: 上面的方法是从另一个方法调用的,如下所示:
def notificationPrefsByFruit = mapMyNotificationsByFruits(prefs, fruits)
When I debug on first line in mapMyNotificationsByFruits
method, I get prefs
as 当我在
mapMyNotificationsByFruits
方法的第一行上调试时,得到的prefs
为
MainNotification [someId=ABC123, email=abc@gmail.com, fruits=[{fruitId=XYZ123, someField=0}]]
On running this code, I get following error: 在运行此代码时,出现以下错误:
groovy.lang.MissingMethodException: No signature of method: com.somepackage.SomeClass$_mapMyNotificationsByFruits_closure5$_closure10.doCall() is applicable for argument types: (groovy.json.internal.LazyMap) values: [[{fruitId=XYZ123, someField=0}]]
What is wrong here ? 这是怎么了
What does these line do: 这些行是做什么的:
MyNotification an ->
def ex = map[(an.fruitId)]
if (!ex) ex = []
ex.add(an)
map[(an.fruitId)] = ex
Is it a casting issue? 这是强制性的问题吗?
Replacing above lines with below code resolves the issue: 用下面的代码替换上面的行可以解决此问题:
MyNotification an = it
def ex = map[(an.fruitId)]
if (!ex) ex = []
ex.add(an)
map[(an.fruitId)] = ex
But I am not sure if both code blocks are same and I am fixing it correctly. 但是我不确定两个代码块是否相同并且我正在正确修复它。
Thanks in advance! 提前致谢!
What does the Algorithm do? 该算法做什么?
Given list of MainNotification
instances it is grouping the MyNotification
instances by fruitId
. 给定
MainNotification
实例的列表, MainNotification
按fruitId
将MyNotification
实例分组。
Given : 鉴于 :
[
[
someId: "ABC123",
email: "abc@gmail.com",
fruits: [
[fruitId : "XYZ123", someField: 0],
[fruitId : "XYZ124", someField: 6],
]
],
[
someId: "XYSK",
email: "yax@gmail.com",
fruits: [
[fruitId : "XYZ123", someField: 5],
[fruitId : "XYZ124", someField: 2],
[fruitId : "XYZ144", someField: 9],
]
]
]
Expected Output: 预期产量:
[
XYZ123 : [
[fruitId : "XYZ123", someField: 0],
[fruitId : "XYZ123", someField: 5]
],
XYZ124 : [
[fruitId : "XYZ124", someField: 6],
[fruitId : "XYZ124", someField: 2]
],
XYZ144 : [
[fruitId : "XYZ144", someField: 9]
]
]
Corrections: 更正:
List<MainNotification>
instead of MainNotification
. List<MainNotification>
而不是MainNotification
。 MyNotification
( this is the main fix to your issue btw ). MyNotification
实例( 这是对您的问题btw的主要修复 )。 You did not post the source code for MainNotification
and MyNotification
so per the input data you gave on your question I modeled them like this: 您没有发布
MainNotification
和MyNotification
的源代码,因此根据您对问题提供的输入数据,我对它们进行了如下建模:
class MainNotification{
String someId;
String email;
List<MyNotification> fruits;
}
class MyNotification{
String fruitId;
int someField;
}
With the corrections and assumptions here is the working code: 经过更正和假设,以下是工作代码:
def mapMyNotificationsByFruits(prefs,fruits) {
def map = [:]
prefs.each { MainNotification cn ->
cn.fruits.each { MyNotification an ->
def ex = map[(an.fruitId)]
if (!ex) ex = []
ex.add(an)
map[(an.fruitId)] = ex
}
}
println ("map is: $map")
return map
}
MainNotification mainNotification = new MainNotification(someId: "ABC123",
email: "abc@gmail.com",
fruits: [new MyNotification(fruitId : "XYZ123", someField: 0)]
);
MyNotification fruits = null; //never used on the method
List<MainNotification> prefs = [mainNotification];
def notificationPrefsByFruit = mapMyNotificationsByFruits(prefs, fruits)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.