def MapTest = [:]
List b = [["abc",1.0], ["def",20.0], ["abc",300.0]]
MapTest = b.groupBy {it[0]}.collectEntries {[(it.key): it.value.sum {it[1]}]}
I am trying to get the result -> [abc:301.0, def:20.0]
This code works fine in my groovy console. But somehow it's not working in an oracle product. I get the below error. If I remove the [0] and [1] it validates, but the results are obviously wrong.
Cannot find matching method java.lang.Object#getAt(int). Please check if the declared type is right and if the method exists.
Maybe it's the way i am arriving at the final list thats creating the issue?? Pasting my sample code here.
def MappingKey = '"'+"a"+"-"+"b"+"-"+"c"+"-"+"d"+'"'
def MappingKey1 = '"'+"a"+"-"+"b"+"-"+"c"+"-"+"d"+'"'
def Data = 1
def Data1 = 5
List a = []
List b = []
a.add("$MappingKey,$Data")
a.add("$MappingKey1,$Data1")
a.each
{
b.add("["+it+"]") // [["a-b-c-d",1], ["a-b-c-d",5]]
}
println b.groupBy {it[0]}.collectEntries {[(it.key): it.value.sum {it[1]}]}
getAtin your code (both[...]) - is there some relation between the error message and which one of the two fails?b.groupBy { k, _ -> k }.collectEntries {[it.key, it.value.sum { _, v -> v }]}