Groovy sample code

Printing a collection objects in groovy

Search for: Printing a collection objects in groovy


//from a list
def list = ["A", "B", "C"]
for (item in list) {
   println item
}

//another
for (number in 1..3 ) {
    println number
}

//a map
def map = [a1:'b1', a2:'b2']
for ( item in map ) {
    println item.key 
}

//each syntax using closures
def list = ["A", "B"]
list.each {
	println it
}

//syntactic sugar
(1..3).each {
	println it
}

//with indexed closures
def list = ["A", "B", "C"]
list.eachWithIndex { val, idx ->
   println "${idx}. ${val}"
}

working with maps in groovy

Search for: working with maps in groovy

Groovy program structure is here


package com.ai.groovy.learn.test1

class GroovyTest2 {

   def propMap = [:]
   
   def String get(String name)
   {
      propMap[name];
   }
   def String set(String name, String value)
   {
      propMap[name] = value;
      println "set method called ${name}:${value}"
   }
   
   static void main(String... args)
   {
      println "hello world"
      
      GroovyTest2 gc = new GroovyTest2();
      gc.p1 = "hello"
      gc.p2 = "why"
      
      println "done ${gc.p2}"
   }
}

How are get and set method are resolved dynamically in groovy?

Search for: How are get and set method are resolved dynamically in groovy?

what is def in groovy?

Search for: what is def in groovy?

This is explained in style guide


def mylist = []
def mymap = [:]
By default keys are strings
Strings don't have to be quoted as a key
def map = [CA: 'California', MI: 'Michigan']

def var1;
var1 = "string";
var1 = 5

//similar to
Object var1;

In the code above the def is redundant!!!


package com.ai.groovy.learn.test1

class GroovyTest2 {

   private def propMap = [:]
   
   String get(String name)
   {
      propMap[name];
   }
   
   String set(String name, String value)
   {
      propMap[name] = value;
      println "set method called ${name}:${value}"
   }
   
   static void main(String... args)
   {
      println "hello world"
      
      GroovyTest2 gc = new GroovyTest2();
      gc.p1 = "hello"
      gc.p2 = "why"
      
      println "done ${gc.p2}"
   }
}

abstract class Abstract {         
    String name

    abstract def abstractMethod() 

    def concreteMethod() {
        println 'concrete'
    }
}

1. The "def" means the method returns an object of any type

2. the last line apparently returns without a return

return type of println in groovy

Search for: return type of println in groovy

Methods in groovy are explained here as part of object orientation

println returns void


package com.ai.groovy.learn.test1

class GroovyTest2 {

   private def propMap = [:]
   
   String get(String name)
   {
      propMap[name];
   }
   
   String set(String name, String value)
   {
      propMap[name] = value;
      println "set method called ${name}:${value}"
   }
   
   def f1()
   {
      println "f1 called"
   }
   static void main(String... args)
   {
      println "hello world"
      
      GroovyTest2 gc = new GroovyTest2();
      gc.p1 = "hello"
      gc.p2 = "why"
      
      println "done ${gc.p2}"
      
      //it seem to return a null
      println gc.f1()
   }
}

On Groovy at Quotra

How are gets and sets resolved in Groovy?

Search for: How are gets and sets resolved in Groovy?

Method call conventions and omitting parentheses is explained in the style guide. Here is the link again


//Same
println ("hello")
println "hello"

method(a,b)
method a, b

list.each( { println it } )
list.each(){ println it }
list.each  { println it }

class Person {
    String name
}

class Server {
    String name
    Cluster cluster
}


def server = new Server(name: "Obelix", 
                cluster: aCluster)

server.name = application.name
server.status = status
server.sessionCount = 3
server.start()
server.stop()

vs:

server.with {
    name = application.name
    status = status
    sessionCount = 3
    start()
    stop()
}

x = "xstring"

//wrong: will print abc $(x)
//because it is not in double quotes
print 'abc $(x)'

print "abc $(x)"

//still wrong because you need to use {} and not ()

//correct one

print "abc ${x}"

//not that will print correctly

abc xstring