Groovy for Java developer: learning Groovy-specific features

Groovy is an object oriented dynamic language for the JVM. Dynamically compiled to a bytecode and interoperable with most Java libraries, Groovy is a perfect candidate for writing lightweight scripts that still can benefit from the Java ecosystem. Also, it is really easy to learn since the good old Java syntax is usually valid in Groovy. There are, however, a few caveats that you should be aware of before start coding. This post shows some specific features, and also gives a few suggestions which will help to write a clear, groovy-style code.

Groovy beans

Groovy beans can be declared in a very simple way. A typical Java bean consists of a constructor, private properties, and getter / setter methods. A Groovy bean should contain only fields, whereas getters and setters will be created implicitly:

class User {
    String firstName // bean field
    String lastName  // bean field
}Code language: JavaScript (javascript)
def user = new User()
user.firstName = "John"
user.lastName = "Doe"Code language: JavaScript (javascript)

Here, the user.firstName and user.lastName assignments are in fact done through the setter call. If needed, you can declare any custom getter and setter for a field, for example:

class User {

    String firstName

    String getFirstName() {
        "The first name is $firstName"
    }

    void setFirstName(String firstName) {
        this.firstName = "[$firstName]"
    }

    // ...
}Code language: JavaScript (javascript)
user.firstName = 'John'
println user.firstNameCode language: JavaScript (javascript)

This results in a string which contains the brackets added by the setter and the “The first name is ” prefix from the getter:

The first name is [John]Code language: CSS (css)

If you provide an access modifier for a field, you will just create a field, the getter / setter will not be added.

Groovy closures

Groovy offers a way to create functions that are first class objects – Groovy closures. Using the closures, you can define a code block and then pass it around as if it is a regular variable:

// the simplest closure
def hello = {"Hello, $it!"}
assert hello('Chris') == 'Hello, Chris!'

// the closure that do not take any params
def bye = {->'Bye!'}
assert bye() == 'Bye!'

// the closure with several params
def niceHello = {firstName, lastName -> "Hello, $firstName $lastName!"}
assert niceHello('Chris', 'Bennett') == 'Hello, Chris Bennett!'Code language: JavaScript (javascript)

As you can see, a closure takes one it parameter by default, you can change this behaviour by specifying named parameters, or omitting parameters altogether with {-> ...} declaration.

Groovy lists and maps

Groovy provides a build-in support for Lists and Maps, and allows to initialize and manipulate the data structures in a way very similar to JavaScript. At first, let’s have a look at the List implementation, you can create an empty
list just be declaring two square brackets:

// an empty list
def emptyList = []

// predefined list
def list = ['One', 'Two', 'Three']Code language: PHP (php)

Or get an element or the range of elements from a list:

def list = ['One', 'Two', 'Three']

// gets the first element from the list
assert list[0] == 'One'

// gets a range of elements from the list
assert list[1..2] == ['Two', 'Three']

// gets another range
assert list[-1..-2] == ['Three', 'Two']Code language: PHP (php)

Also, there are available several handy operations for iterating and transforming the lists:

// iterates the list
def emptyList = []
list.each {emptyList << "$it!"}
assert emptyList == ['3!', '1!', '2!']

// iterates the list and transforms each entry into a new value
// using the closure
assert list.collect {it * 2} == [6, 2, 4]

// sorts using the closure as a comparator
assert list.sort {it1, it2 -> it1 <=> it2} == [1, 2, 3]

// gets min or max using closure as comparator
assert list.min {it1, it2 -> it1 <=> it2} == 1Code language: PHP (php)

The Map implementation has a lot of syntactic sugar as well. An empty map can be declared with two square brackets and a colon:

// an empty map
def emptyMap = [:]

// predefined map
def map = [John: 10, Mark: 20, Peter: 'Not defined']Code language: JavaScript (javascript)

You can get a value from a Map using two styles — the bean style or the associated array style:

def map = [John: 10, Mark: 20, Peter: 'Not defined']

// the array style
assert map['Peter'] == 'Not defined'

// the bean style
assert map.Mark == 20

// also you can preset default value that will be returned by
// the get method if key does not exist
assert map.get('Michael', 100) == 100Code language: JavaScript (javascript)

And perform iterating and transforming with the help of closures:

// iterates the map
def emptyMap = [:]
def map = [John: 10, Mark: 20, Peter: 'Not defined']
map.each { key, value ->
    emptyMap.put key, "$key: $value" as String
}
assert emptyMap == [John: 'John: 10', Mark: 'Mark: 20', 
        Peter: 'Peter: Not defined']

// iterates the map and transforms each entry using
// the closure, returns a list of transformed values
assert map.collect { key, value ->
    "$key: $value"
} == ['John: 10', 'Mark: 20', 'Peter: Not defined']

// sorts map elements using the closure as a comparator
map.put 'Chris', 15
assert map.sort { e1, e2 ->
    e1.key <=> e2.key
} == [John: 10, Chris: 15, Mark: 20, Peter: 'Not defined']Code language: JavaScript (javascript)

Other helpful constructs

  • boolean operator == works the same as Java equals for all types. Unlike Java where == means equality for primitive types and identity for objects. To compare by identity, use the is method instead:
assert [] == []
assert ![].is([])
  • the .* operator is introduced to perform operations on all the members of a collection:
assert [1, 2, 3] == ['a', 'ab', 'abc']*.size()Code language: JavaScript (javascript)
  • the return keyword is optional at the end of the methods:
def foo() {
    // do something
    return something
}
// is equals to
def bar() {
    // do something
    something
}Code language: JavaScript (javascript)

Conclusion

There are a lot of other interesting features: metaprogramming, functional programming techniques, AST transformations, you will surely discover them all along the way. The power of Groovy is that you can start with a basic Java syntax, and then gradually learn how to improve the code until it becomes truly fluent and dynamic.

Software Developer & Technical Lead