Tuesday, October 2, 2012

Say hello to Groovy - Part 3 (Collections)


In my previous blog i talked about various operators and control structures available in Groovy. Today i am going to talk about Groovy collections. Like Java, we have List and Map in Groovy. Groovy also has something called Range.

 
Lists

List is an ordered collection of objects. Groovy lists are indexed using [] operator. We can use [] to define an empty list and << to add items to a list.

Following code snippet shows how a list is defined in Groovy.
def numbersList = [9, 2, 99, 22] // list with four items
println numbersList [0] // 9
println numbersList [3] // 22
println numbersList [-1] // 22 , If the integer index is negative, then it refers to elements by counting from the end.
view raw list1.groovy hosted with ❤ by GitHub

List Methods

Lists support many useful methods. Following code snippet illustrate the usage of some of the list methods.

def numbersList = [10,20,30,40]
numbersList.add(50)
println numbersList //[10, 20, 30, 40, 50]
numbersList.add(3, 25)
println numbersList //[10, 20, 30, 25, 40, 50]
numbersList.add([50, 60])
println numbersList //[10, 20, 30, 25, 40, 50, [50, 60]]
println numbersList.get(1) //20
println numbersList.isEmpty() //false
println numbersList.size() //7
println numbersList.getAt(1) //20
println numbersList.intersect([30, 25, 40]) //[30, 25, 40]
println numbersList.pop() //[50,60]
println numbersList.reverse() //[50, 40, 25, 30, 20, 10]
println numbersList.sort() //[10, 20, 25, 30, 40, 50]
println ([2,9,[22,99]].flatten()) //[2, 9, 22, 99]

Maps
Map is a collection of key value pair. We can use [:] to define an empty map. If the keys in a Map is String, it can act as a bean.
Following code snippet shows how to define and use Maps in Groovy.

def employee = ['Finance' : 'John', 'Operations' : ['Mary','Adam'], 'HR' : 'Melissa']
println employee['Finance'] // John
println employee.Finance // John
println employee['IT'] // null
println employee['Operations'] // [Mary, Adam]
view raw map1.groovy hosted with ❤ by GitHub

Map Methods 
Maps support many useful methods. Following code snippet illustrate the usage of some of the Map methods.

def employeeSalary = ['John' : 3000, 'Mary' : 4500, 'Adam' : 1500, 'Melissa':2500]
employeeSalary.put('Neil', 6000)
println employeeSalary // [John:3000, Mary:4500, Adam:1500, Melissa:2500, Neil:6000]
println employeeSalary.containsKey('Mary') // true
println employeeSalary.get('Adam', 5000) // 1500
println employeeSalary.get('Sally',5000) //5000
println employeeSalary.get('Adam') // 1500
println employeeSalary.get('Sally') //null
println employeeSalary.get('Melissa') // 2500
println employeeSalary.keySet() // [John, Mary, Adam, Melissa, Neil]
println employeeSalary.size() // 5
println employeeSalary['John'] // 3000
view raw map2.groovy hosted with ❤ by GitHub

Range

Range can be used to specify a list of sequential values. Range is defined using the first and last value of the sequence. Range can also be defined to exclude the last value. Use .. operator to include first and last value. Use ..< operator to exclude last value.

Ranges can be used for any Java object which implements java.lang.Comparable for comparison and also have methods next () and previous() to return the next / previous item in the range.

follwoing code snippet shows how to define and use Range in Groovy.
println 1..10 // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
println 1..<10 // [1, 2, 3, 4, 5, 6, 7, 8, 9]
println 'a'..'f' // [a, b, c, d, e, f]
println 10..1 // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
println 'Z'..'U' // [Z, Y, X, W, V, U]
view raw range1.groovy hosted with ❤ by GitHub

Range Methods

Like Lists and Maps, Range also support many useful methods. Following code snippet illustrate the usage of some of the Range methods.

def oneToTen = 1..10
def tenToOne = 10..1 // Reversed Range
println oneToTen.size() // 10
println oneToTen.get(0) // 1
println oneToTen.getFrom() // 1
println oneToTen.getTo() // 10
println oneToTen.contains(2000) // false
println oneToTen.subList(0, 5) // [1, 2, 3, 4, 5]
println tenToOne[2] // 8
println tenToOne.isReverse() // true
view raw map2.groovy hosted with ❤ by GitHub









No comments:

Post a Comment