Groovy
- It is an agile and dynamic language for the Java Virtual Machine
- builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk
- makes modern programming features available to Java developers with almost-zero learning curve
- provides the ability to statically type check and statically compile your code for robustness and performance
- supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain
- makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL
- increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications
- simplifies testing by supporting unit testing and mocking out-of-the-box
- seamlessly integrates with all existing Java classes and libraries
- compiles straight to Java bytecode so you can use it anywhere you can use Java
Sample Programs
class TestMe {
static void main(def arg)
{
def a=[1,2,3,4,5,6]
a.each {e -> println e*2} //Lamda expression
}
}
For Each
class TestMe {
static void main(def arg)
{
def a=[1,2,3,4,5,6]
for(m in a)
println m
}
}
Sum Values
class TestMe {
static void main(def arg)
{
def age=[1,2,3,4,5,6]
def total=0
for(m in age)
{
//total=total+m
total+=m
}
println "Total is $total"
}
}
Sum Values
class TestMe {
static void main(def arg)
{
def ages=[1,2,3,4,5,6]
def total = ages.inject(0){carry,age -> carry+age}
println "Total is $total"
}
}
Sum Values
class TestMe {
def totalValues(ages)
{
def total=0
for(e in ages)
{
total+=e
}
total
}
static void main(String[] arg)
{
TestMe t=new TestMe();
def ages=[1,2,3,4,5,6]
println t.totalValues(ages)
}
}
Sum Values using Closure -1
class TestMe {
def totalValues(ages,selector)
{
def total=0
for(e in ages)
{
if(selector(e))
total+=e
}
total
}
static void main(String[] arg)
{
TestMe t=new TestMe();
def ages=[1,2,3,4,5,6]
println t.totalValues(ages,{true})
}
}
Sum Values using Closure -1I using lamda expression
class TestMe {
def totalValues(ages,selector)
{
def total=0
for(e in ages)
{
if(selector(e))
total+=e
}
total
}
static void main(String[] arg)
{
TestMe t=new TestMe();
def ages=[1,2,3,4,5,6]
println t.totalValues(ages,{true}) //Sum all values
println t.totalValues(ages,{e -> e%2==0}) //Sum Odd values
println t.totalValues(ages,{e -> e%2!=0}) //Sum Even values
println t.totalValues(ages,{ it%2!=0}) //Summ Even values
}
}
Factorial
def factorial(n){
def fact=1
for(i in 1..n)
{
fact*=i
}
fact
}
println factorial(5)
Factorial in functional style
def factorial(n){
(1..n).inject(1) {fact,e -> fact *e}
}
println factorial(5)
Doubled Array -1
def numbers=[1,2,3,4,5,6]
def doubled=[]
numbers.each {doubled << it*2}
println doubled
Doubled Array -2
def numbers=[1,2,3,4,5,6]
def dobuled =numbers.collect { it*2}
println dobuled
Different style program
def closures=[]
for(i in 1..3)
{
closures << {println i}
}
for(closure in closures)
{
closure();
}
Different style program
def closures=[]
(1..3).each{i -> closures <<{println i}}
for(closure in closures)
{
closure();
}
Double the array -1
def doubledIt(numbers)
{
def doubled=[]
for(e in numbers){
//if(e%2==0)
doubled << e*2
}
doubled
}
def values=[1,2,3,4,5,6]
println doubledIt(values)
Double the array -2
def doubledIt(numbers)
{
numbers.collect{it * 2}
}
def values=[1,2,3,4,5,6]
println doubledIt(values)
Double the array for odd numbers
def doubledIt(numbers)
{
numbers.findAll{it%2==0}.collect{it * 2}
}
def values=[1,2,3,4,5,6]
println doubledIt(values)
Class example -1
class Book
{
String title
int pages
}
def create(klass,properties)
{
def instance =klass.newInstance()
properties.each { name , value ->
instance."$name" = value
}
instance
}
//println create(Book);
//println create(Book).dump();
println create(Book,[title: 'Kadhai',pages: 30]).dump()
Class example -2
class Book
{
String title
int pages
}
class CD
{
String title
int volume
}
def create(klass,properties)
{
def instance =klass.newInstance()
properties.each { name , value ->
instance."$name" = value
}
instance
}
println create(Book,[title: 'Kadhai',pages: 30]).dump()
println create(CD,[title: 'Madras',volume: 3]).dump()
Iterate Key Value
def info=[name:'thamil',age:18,place:'anbil']
println info.collect{k,v -> "$k=$v"}.join("&")
Add value into Collection
def list=[]
list<<1
or
list=[1,2,3,4,6]
or
list.add(1)
Data Exists in collection or not
def list=[eno:1,ename:'thamil']
def list=[]
//println 'ename' in list
if(map)
println "Data is there"
else
println "No Data is there"
Conditional Operator
def list=[eno:1,ename:'thamil']
def list1=[]
def var=false
println var ? 1 : 0
println var ? Var : [1,2,3] --- 1
println var ? : [1,2,3] --- 2
Above both statements are same
Null Handling
String a=null;
println a.length(); //It will throw null pointer exception
println a?.length(); // It will print null value
Find Class Name
String a="thamil";
println a.class.name
println a
Concat and Find Class Name
def b="vanan"
def a="thamil $b";
println b.class.name
println a.class.name
print a
Concat and Find length
def b="vanan"
def a="thamil ${b.length()}";
print a
Multiline Template “””
def b="vanan"
def a="""thamil ${b.length()}
abc
def
gh
""";
print a
Closure
def x={
println "Welcome to india"
}
x()
Closure – Passing value through method
def x={string ->
println "$string Welcome to india"
}
x()
x("Mr.Thamil")
ClosureSimple Consolitated programs
def list =[1,2,3,4,5]
Program 1
for(int i=0;i<list.size();i++)
println i
Program 2
for(x in list)
println x
Program 3
list.each{
println it
}
Program 4
def number=3
number.times{
println "hi"
}
Program 5
println list.collect{
it >2?it:null
}
Program 6
println list.collect{
it %2
}
Program 7
println list.findAll{
it %2==0
}
Program 8
println list.findAll{
it>2?:false
}
Program 9
println list.findResults{
it>2?"Number $it":null
}
Program 10
println list.inject {accumulated ,value -> accumulated + value}
Method Call
def mymethod(arg1,arg2)
{
println "$arg1 $arg2"
}
mymethod(10,20)
Method Call with default value
def mymethod(arg1,arg2=30)
{
println "$arg1 $arg2"
}
mymethod(10,20)
mymethod(10)