Posted By

hansamann on 02/22/07


Tagged

maps groovyseries


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

jsbournival
bootz15
hendersk
mbanfi
adaptives


Groovy Series: Maps


 / Published in: Groovy
 

URL: http://hansamann.podspot.de/files/grails_podcast_episode_37.mp3

This snippet gives you some interesting examples what you can do with Groovy Maps. Listen to the audio to get the best learning experience. Simply right-click on the title above to download the mp3 file of this part of the series.

The Groovy Series is part of the Grails Podcast and can be subscribed to via: http://hansamann.podspot.de/rss. The series is produced by Dierk K├Ânig and Sven Haiges, further information about the topic of this episode can be found in the book

Groovy in Action - http://groovy.canoo.com/gina

  1. def empty = [:]
  2. assert empty.size() == 0
  3.  
  4. //keys are Strings by default
  5. assert [key:'value'] == ['key':'value']
  6.  
  7. //you must use parentheses if a variable should become a key
  8. def myVar = "theKey"
  9. def myMap = [(myVar):'someValue']
  10. assert myMap['theKey'] == 'someValue'
  11.  
  12. //you can use array-style notation or bean-style notation to access and set entries
  13. def map = [:]
  14. //array-style
  15. map['a'] = 1
  16. assert map['a'] == 1
  17.  
  18. //bean-style
  19. map.b = 2
  20. assert map.b == 2
  21.  
  22. //using a default value in case the key is not available
  23. def someMap = [a:1,b:2]
  24. assert someMap.get('a', -1) == 1
  25. assert someMap.get('c', -1) == -1
  26. assert someMap.get('c') == -1 //TAKE CARE!
  27. assert someMap.get('d') == null
  28.  
  29. //iterating over a map with each() -> "it" is an entry with properties key and value
  30. def iterationMap = [a:1,b:2,c:3]
  31. def keyList = []
  32. iterationMap.each { keyList << it.key }
  33. assert keyList.contains('a')
  34. assert keyList.contains('b')
  35. assert keyList.contains('c')
  36.  
  37. //or split the entry into key and value directly
  38. iterationMap.each { key, value -> keyList << key}
  39. assert keyList.size() == 6
  40.  
  41. //get all keys of a map
  42. def keys = [a:1].keySet()
  43. assert keys.size() == 1
  44. assert keys.contains('a')
  45. assert ['a'] as Set == [a:1].keySet()
  46. assert ['a'] == [a:1].keySet().toList()
  47.  
  48. //you can use findAll with maps, too
  49. def books = [ 'Graeme Rocher':'The Definitive Guide to Grails',
  50. 'Dierk Koenig':'Groovy in Action']
  51. def subMap = books.findAll { key, value -> key == 'Graeme Rocher'}
  52. assert subMap.'Graeme Rocher' == 'The Definitive Guide to Grails'
  53.  
  54. //and collect... collect does a transformation with each entry
  55. def list = books.collect { key, value -> (key == 'Dierk Koenig')? 'GINA' : value }
  56. assert list.join(',') == 'GINA,The Definitive Guide to Grails'

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: hansamann on April 25, 2007
Posted By: bootz15 on April 4, 2008

So close! The last line is actually: assert list.join(',') == 'The Definitive Guide to Grails,GINA' Thanks for the snippets though :)

Posted By: bootz15 on April 4, 2008

So close! The last line is actually: assert list.join(',') == 'The Definitive Guide to Grails,GINA' Thanks for the snippets though :)

You need to login to post a comment.