Seb
Seb

Reputation: 1551

Convert sql.GroovyRowResult with two columns to map

I have a groovy GroovyRowResult (using sql.rows) with two columns. The first column has a group by in the sql, so it will always be unique.

Now I would like to convert the whole result into a map, which contains the first column value as the key and the second column value as the value. I know that I can do this with each for every single row (see below), but was wondering if there is a better aproeach. The only thing I've found is entrySet(), which did not work. Error is:

Message: No signature of method: 
java.util.ArrayList.entrySet() is applicable for argument types: () values: [])

So my each row way would be:

def myMap = [:]
result.each {
    myMap.put(it.colum1, it.column2)
}

Another alternative would be, to make the query in a way that will return me a map with the first column as the key and the second as the value (or the whole row as the value). Kind of Zend frameworks fetchAssoc() method.

Upvotes: 2

Views: 4501

Answers (1)

tim_yates
tim_yates

Reputation: 171114

You could do:

def myMap = result.collectEntries {
    [ it.column1, it.column2 ]
}

Upvotes: 6

Related Questions