I am new to Groovy. I execute the following Groovy code:
myList=[234, 34, "Stackoverflow", 3.14]
myList=myList.collect {if (it instanceof Integer) it*it}
println myList
it outputs:
[54756, 1156, null, null]
It seems to me that it shouldn’t change the strings value. when I change the second line to:
myList=myList.collect {if (it instanceof Integer) it*it else it=it}
it works as I expected:
[54756, 1156, Stackoverflow, 3.14]
I am wondering what is the logic behind that!
Since there is no
elseclause in your first version,nullis the result.The second version should work like this, too: