GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts

Monday, December 20, 2010

Iteration In Groovy

To iterate over a list, use:
groovy>> data = ["luna maya", "lady gaga", "steve jobs"]
groovy>> data.each {
groovy>>     println it
groovy>> }

to iterate over a map, use:
groovy>> datas = ["lady": "gaga", "bill": "gates", "larry": "page"]
groovy>> datas.keySet.each {
groovy>>     println it+": "+datas[it]   // it hold the key.
groovy>> }
Share/Bookmark

Conditional Structure In Groovy

Use conditional structure, use:
groovy>> a = 2>3
groovy>> if a {
groovy>>     println "The first block executed";
groovy>> }else{
groovy>>     println "The second block executed";
groovy>> }
Share/Bookmark

Map In Groovy

Groovy has a built in map data structure. To define it, use:
groovy>> data = ["bill": "gates", "steve": "jobs", "aura": "kasih"]

If you want to add an element to the map, use:
groovy>> data["lady"] = "gaga"
or
groovy>> data.lady = "gaga"

To access an element of the map, use:
groovy>> println data["lady"]
or
groovy>> println data.lady

To get the size of the data, type:
groovy>> data.size()

To create an empty map, type:
groovy>> data = [:]


Share/Bookmark

List In Groovy

Groovy has built in datastructure called list. It used to store ordered data. To initialize it, use:
groovy>> name = ["lady gaga", "luna maya", "barack obama"]

Use [ ] operator to access an element of the data:
groovy>> println name[0]
groovy>> println name[2]

To get the size, use:
groovy>> println name.size()

You can add an element to it by:
groovy>> data[5] = "bill clinton"

You cannot do this, because there's not index number
groovy>> data[] = "tony blair"

To create an empty list, type:
groovy>> data = []
Share/Bookmark

The First In Groovy

Run Groovy shell in bin directory by: This is a mode of the Groovy environment:
>> groovysh.exe

To test that your environment, type:
groovy>> println 'Holla World' 

groovy>> x = 45
groovy>> println x

groovy>> y = false
groovy>> println y

groovy>> x = new java.util.Date()
groovy>> println x

Share/Bookmark