JavaDevNotes.com

Groovy Map Examples

The trend in the software development industry is going in the direction of making things easier and simpler. This is why languages such as Groovy is getting popular as time pass by. And there has been a big surge in popularity of Groovy when Gradle became the recent standard in Build Automation System. Groovy is just hands down concise and simpler than Java and makes developer time so much more efficient. In this post, we will explore how to use Groovy Maps by providing examples for different use cases.

Groovy Map Declaration

Most developers who try to learn Groovy came from Java background. And this is how we declare Map in Java and instantiate an empty instance.
Map<String,String> myMap = new HashMap<String,String>();
We are saying here that we want a Map with String as both key and value. And that we want to use HashMap as an implementation.

As Groovy is the more concise and efficient programming language, the counterpart declaration of Map in Groovy looks like this.

def myMap = [:]
As shown, the Groovy way of declaring and creating an empty Map is short and simple. And it is easy to understand at a glance. A square bracket with colon in the middle creates an instance of an empty map in Groovy. Simple square brackets just means a List. Adding the colon differentiates from List to Map.

In Groovy, it is always understood that the key is a String object and the value can be of any type.

Groovy Map Initial Value Value

Declaring a map with an initial value is very simple. See code below:

def myMap = [firstName:'John', lastName:'Doe', greeting:'Hello There!']

And without too much explanation, we can understand what the code above means by just a mere glance. We see that the map contains three items, with the following mapping
  • firstName is John
  • lastName is Doe
  • greeting is Hello World!

Groovy Map Put Key And Value

Adding items to a Map in Java looks like the code below. We use specific code put that takes two parameters. The first is the key and the second is the value.

Map<String,String> myMap = new HashMap<String,String>();
myMap.put("firstName", "John");
myMap.put("lastName", "Doe");
myMap.put("greeting", "Hello There!");

Although the example above is simple and short, the Groovy equivalent is even shorter and more understandable. See example below:
def myMap = [:]
myMap['firstName'] = 'John'
myMap['lastName'] = 'Doe'
myMap['greeting'] = 'Hello There!'
Another way of adding mapping is shown below:
def myMap = [:]
myMap.firstName = 'John'
myMap.lastName = 'Doe'
myMap.greeting = 'Hello There!'
And also another way
def myMap = [:]
myMap.'firstName' = 'John'
myMap.'lastName' = 'Doe'
myMap.'greeting' = 'Hello There!'

And because Groovy is a superset of Java, we can also put key value pair to Groovy Map using below:

def myMap = [:]
myMap.put('firstName', 'John');
myMap.put('lastName', 'Doe');
myMap.put('greeting', 'Hello There!');

Groovy Map Replace Value

Assuming we have the map below:

def myMap = [firstName:'John', lastName:'Doe', greeting:'Hello There!']

And we wish to replace the values, we just need to put new value similar to how we add new entries to the map. For example:

def myMap = [firstName:'John', lastName:'Doe', greeting:'Hello There!']
myMap.firstName = 'Jane'
myMap.lastName = 'Smith'
myMap.greeting = 'Hi, How are you?'

And the previous value is now overwritten.

Groovy Map Lookup Value

Here are some examples on how to lookup values mapped to a key in a map. Below is to show that we can use same method we use in Java.
def myMap = [firstName:'John', lastName:'Doe', greeting:'Hello There!']
println myMap.get('firstName');
println myMap.get('lastName');
println myMap.get('greeting');

The get method will do the lookup and the println will output the value to the console. A similar code is shown below:

def myMap = [firstName:'John', lastName:'Doe', greeting:'Hello There!']
println myMap['firstName'];
println myMap['lastName'];
println myMap['greeting'];

And yet another way:

def myMap = [firstName:'John', lastName:'Doe', greeting:'Hello There!']
println myMap.'firstName';
println myMap.'lastName';
println myMap.'greeting';

And lastly:

def myMap = [firstName:'John', lastName:'Doe', greeting:'Hello There!']
println myMap.firstName;
println myMap.lastName;
println myMap.greeting;