0
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]}]}
9
  • "In an oracle product" - and what could that be? Is there a stacktrace or any further information, where the error is happening? You have to getAt in your code (both [...]) - is there some relation between the error message and which one of the two fails? Commented Sep 10, 2020 at 20:27
  • 1
    And you could try destructuring: b.groupBy { k, _ -> k }.collectEntries {[it.key, it.value.sum { _, v -> v }]} Commented Sep 10, 2020 at 20:29
  • Thank for the reply. Oracle PBCS. Both fails. there are two error messages for [0] and [1] - same message. Commented Sep 10, 2020 at 20:31
  • @cfrick is there any other way I can achieve the same, if you can give me an idea. Cant seem to get this to work. My whole code is dependant on this one line of code. Commented Sep 10, 2020 at 21:05
  • I have suggested an alternative. Have you tried it and does it not work? Commented Sep 11, 2020 at 5:38

1 Answer 1

2

seems you have kind a CompileStatic groovy transformer activated in oracle

the following code will give you an error in console:

//this throws exception getAt not found

@groovy.transform.CompileStatic
def f(List b){
    return b.groupBy {it[0]}.collectEntries {[(it.key): it.value.sum {it[1]}]}
}

f( [["abc",1.0], ["def",20.0], ["abc",300.0]] )

however with full type definition List<List> the same line works fine:

@groovy.transform.CompileStatic
def f(List<List> b){
    return b.groupBy{it[0]}.collectEntries {[(it.key): it.value.sum {it[1]}]}
}


f( [["abc",1.0], ["def",20.0], ["abc",300.0]] )
Sign up to request clarification or add additional context in comments.

2 Comments

This is the complete error. looks like there is some static checking. I have updated my original question. Error:The Groovy script failed to compile with internal error: Compile Error: [Static type checking] - Cannot find matching method java.lang.Object#getAt(int). Please check if the declared type is right and if the method exists
in modified question b variable is a List of Strings and not a List of Lists. and if you want to validate compile static in groovy console just add annotation to a function like in code above: @groovy.transform.CompileStatic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.