Posted By

hansamann on 02/22/07


Tagged

lists groovyseries collections


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

jsbournival
bootz15
hendersk
mbanfi
adaptives
michaelss


Groovy Series: Lists


 / Published in: Groovy
 

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

This snippet gives you some interesting examples what you can do with Groovy Lists. 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. //Lists
  2.  
  3. //new empty list
  4. def empty = []
  5. assert empty.size() == 0
  6.  
  7. //creating a populated list with different objects
  8. def myList = ["Test", 1, 1..5, null]
  9. assert myList.size() == 4
  10.  
  11. //negative indexes for accessing the final elements
  12. myList = [1,2,3,4,5]
  13. assert myList[-1] == 5
  14. assert myList[-1..-2] == [5,4]
  15. assert myList[0..2] == [1,2,3]
  16.  
  17. //adding sth to it
  18. myList << "AnotherElement"
  19. assert myList.size() == 6
  20.  
  21. //but you can also use the java style
  22. myList.add("something")
  23. assert myList.size() == 7
  24.  
  25. //pop an element
  26. assert myList.pop() == "something"
  27. assert myList.pop() == "AnotherElement"
  28. assert myList.size() == 5
  29.  
  30. //cutting and replacing a part of a list
  31. def list = [0,1,2,3,4]
  32. list[0..2] = [6,5] // different sizes !!
  33. assert list == [6,5,3,4]
  34.  
  35. //creating a new list and reversing it
  36. def someList = [1,2,3,4,5]
  37. assert someList.reverse().join(',') == '5,4,3,2,1'
  38.  
  39. //sorting lists
  40. myList = [10,5,15,25,20]
  41. assert myList.sort { it } == [5,10,15,20,25]
  42.  
  43. //summing up
  44. myList = [10, 20]
  45. assert myList.sum() == 30
  46.  
  47. //convenience: instead of doing reverse().each{ } you can use reverseEach{}
  48. def newList = []
  49. [1,2,3].reverseEach{ newList << it }
  50. assert newList.join(',') == '3,2,1'
  51.  
  52. //you can subtract Lists
  53. assert ( [1,2,3] - [3,2] ).size() == 1
  54.  
  55. //you can add lists
  56. assert ([1,2,3] + [4,5]).size() == 5
  57. //why is plus() not part of the GDK description??? http://groovy.codehaus.org/groovy-jdk.html#cls44
  58.  
  59. //you can multiply lists
  60. assert ([1,2,3] * 2).join(',') == '1,2,3,1,2,3'
  61.  
  62. // you cannot divide lists :-)
  63. //[1,2,3] / [1,2,3] //won't work
  64.  
  65. //of course you can have lists within lists
  66. def listOne = ['a','b','c']
  67. def listTwo = [1,2,3]
  68. listOne << listTwo
  69. assert listOne.size() == 4
  70.  
  71. //flatten will remove the inner lists
  72. assert listOne.flatten().join(',') == 'a,b,c,1,2,3'
  73.  
  74. //Windows only
  75. //as a special bonus, you can execute the elements of a list. First element is the command, others are parameters
  76. //you can do the same with strings by the way ( "cmd /c dir".execute().text )
  77. //def process = ['cmd', '/c', 'dir'].execute()
  78. //println process.text //does print the execution of the dir command

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: hansamann on February 28, 2007

You need to login to post a comment.