Sunday, April 19, 2009

Groovier collections

I have been groovying almost over a year now.There are many remarkable things that groovy do in a very simple and intuitive manner .If you haven't start using it, you are already late.Today , i am going to discuss how groovy takes using collection to another level.Currently, i use it for routine scripts and code generation.
Java Collections framework is one of the most used SDK API (esp. in enterprise applications).This Api allows you do handle ,manipulate and represent the Collection of any Java objects.This greatly reduces programming effort where as boosting programming performance and fostering reuse.
Groovy adds to this many useful operators and tweaks to make working with collection api more entertaining experience.
Following is the ways you create a List in groovy:


L=[0,1,2,3]
println L.class //prints class java.util.ArrayList

This showing that ArrayList is used by groovy behind the scenes to create a List representation.

To work with LinkedList do this

L = new LinkedList([0,1,2,3]);
println L.class


To access, any element from the List , you can use array-like notation familiar to most of Java developers.Other ways to retrieve the elements are get and getAt methods expecting the index (-ve index allowed).

println L[2] //prints 2
println L[-3] //prints 1
println L.get(0) //prints 0
println L.getAt(1) //prints 1

The most frequent operation done on a list is to add elements to it.This can be acheived using +=,<< or using add method.To merge 2 lists use, addAll or + operator.

L = [0,1,2,3]
L += 4
L << 5
L.add(6)
println L //prints [0,1,2,3,4,5,6]
L.addAll(["a","b"])
println L // prints [0,1,2,3,4,5,6,a,b]
K = L + [99,100]
println K // prints [0,1,2,3,4,5,6,a,b.99.100]


Removing an element is equally easy.Use -,-= or call minus.remove() method of list is too supported.

L = [0,1,2,3]
L -= [3]
println L //prints [0,1,2]
L = L.minus(2)
println L //prints [0,1]
L.remove(0)
println L //prints [1]


Reversing the content of the list can be done as

L = [0,1,2,3]
println L.reverse() //prints [3,2,1,0]


Using map is easier than ever, the content left to the : is the key and to the right is the value.

M= ['a':'A','b':'B','c':'C']
println M // prints [a:A,b:B,c:C]
println M.getClass() //prints java.util.LinkedHashMap.We cannot use .class as . is reserved to access the key-values
println new HashMap(['a':'A','b':'B','c':'C']) // prints [a:A,b:B,c:C]


Retreiving value can be done numerous ways.

M= ['a':'A','b':'B','c':'C']
println M.a // prints A
println M['a'] // prints A
println M.get('c') // prints C
println M.getAt('b') // prints B
println M.getAt('x') // prints null as no key is matched.


Adding new key-value pairs too has various alternative.

M= ['a':'A','b':'B','c':'C']
M['d'] = 'D'
println M // prints ['a':'A','b':'B','c':'C','d':'D']
M.put('e','E')
println M // prints ['a':'A','b':'B','c':'C','d':'D','e':'E']
M.putAt('f','F')
println M // prints ['a':'A','b':'B','c':'C','d':'D','e':'E','f':'F']

Remove a pair with remove method paasing the Key.

M= ['a':'A','b':'B','c':'C']
M.remove('b') = 'D'
println M // prints ['a':'A','c':'C']


i'll cover more groovy stuff in upcoming posts.
till then...happy Groovying ;)

P.S : Syntax high lighting help from here.

3 comments:

Unknown 1:09 AM  

Another nice add to the map interface is the ability to specify a default return value in case the key does not appear in the map:

map.get('key', 'defaultValue')

This is especially useful for things like occurrence counting:

map['key'] = map.get('key', 0) + 1

Thanks for the post!

paulk_asert 3:57 AM  

Nice article. You also have this syntax available for a LinkedList:

list = [0, 1, 2, 3] as LinkedList
println list.class.name // => java.util.LinkedList

joune 3:23 PM  

looks so much like javascript, but not exactly.. why yet a new syntax?

while GWT compiles java into javascript, Groovy compiles "javascript" into java.. flavors for all tastes i guess :)

Sociofluid