map Include in Groovy

Find this useful? Support us: Star on GitHub 6
Category: Array | Language: Groovy

In Groovy, we can check if a hash includes a key using the containsKey() method. Here is an example:

def map = [name: 'John', age: 30]

if (map.containsKey('name')) {
println 'The map includes the key "name"'
} else {
println 'The map does not include the key "name"'
}

In this example, we first define a map with two key-value pairs. We then use the containsKey() method to check if the map includes the key 'name'. If the key exists, the message "The map includes the key "name"" will be printed, and if not, the message "The map does not include the key "name"" will be printed.

Alternatively, we can use the keySet() method to get a set of all the keys in the map, and then check if the key we are interested in exists in this set. Here is an example:

def map = [name: 'John', age: 30]

if (map.keySet().contains('name')) {
println 'The map includes the key "name"'
} else {
println 'The map does not include the key "name"'
}

This example produces the same result as the previous one, but uses the keySet() method instead.