0

I have created a LinkedHashMap, which produces a list of statuses in groovy. These are the results of a mysql query. I want to filter the results of my map to use key entries (statusName) in the map which start with the letter "O". However I am struggling to find how to do this with a map.

My code is as follows:

db.eachRow(reportQuery, [date]) {
      cache.put(it.statusName, it.statusTickets)
    }
    cache.each{statusName, statusTickets ->
      reportMetricsWithValues[statusName] = statusTickets
      table.addData([metric:statusName, value: statusTickets])
    }

This is the part of my code where I need to add this filter. The code is adding the key value metrics to a database table.

AD26891
  • 73
  • 2
  • 11

1 Answers1

1

To filter, you would use findAll. Pass it the map element by element and check, if the key of the element starts with the letter O; Something along the lines of:

groovy:000> [zomg: 1, omg: 2].findAll{ it.key.startsWith('o' ) }
===> [omg:2]

If you also need the "others", then groupBy (same syntax like above) could prove useful.

cfrick
  • 35,203
  • 6
  • 56
  • 68
  • Thanks, I tried this, adjusting it to what I have above. But it seems to have diregarded it. db.eachRow(reportQuery2, [nocDate]) { cache.findAll{it.key.startsWith('O')} cache.put(it.statusName, it.statusTickets) } – AD26891 Mar 27 '15 at 09:21
  • if you have furhter informations, please edit the question. it is utterly unreadable in a comment, and quite honest i don't understand, what your ulitmate goal here is. it eve looks you could as well just `select statusName, statusTicket where statusName like 'O%' order by date'`. – cfrick Mar 27 '15 at 09:36