English
Map
About 327 wordsAbout 1 min
2026-01-09
Map - A collection type enclosed with [], containing comma-separated key-value pairs.
Define a Map:
Example:
Map map = ["a":1, "b": 2, "c":3]Map type methods:
- map.keys(): Gets all property names in the dictionary
Return type: List
Example:
Map map = ["a": 1, "b": 2]
result = map.keys() // Returns: ["a", "b"]- map.size(): Returns the number of elements in the dictionary
Return type: BigDecimal
Example:
Map map = ["a": 1, "b": 2]
result = map.size() // Returns: 2- map.isEmpty(): Checks if the dictionary is empty. Returns true if no key-value mappings exist; false otherwise
Return type: Boolean
Example:
Map map = ["a": 1, "b": 2]
result = map.isEmpty() // Returns: false- map.remove(
<String key>): Removes and returns the element with the specified key
Return type: Object
Example:
Map map = ["a": 1, "b": 2]
map.remove("a") // Returns: 1- map.clear(): Removes all key-value pairs from the dictionary
Return type: void
Example:
Map map = ["a": 1, "b": 2]
map.clear()- map.put(
<String key>,<Object value>): Stores a key-value pair
Return type: void
Example:
Map map = ["a": 1, "b": 2]
map.put('c', 3)- map.putIfAbsent(
<String key>,<Object value>): Stores a key-value pair only if the key doesn't exist
Return type: Object
Example:
Map map = ["a": 1, "b": 2]
map.putIfAbsent('a', 2) // The value of key "a" remains 1- map.containsKey(
<String key>): Checks if the dictionary contains the specified key
Return type: Boolean
Example:
Map map = ["a": 1, "b": 2]
map.containsKey("a"); // Returns: true- map.containsValue(
<Object value>): Checks if the dictionary contains the specified value
Return type: Boolean
Example:
Map map = ["a": 1, "b": 2]
map.containsValue(2); // Returns: true- map.values(): Returns a collection of all values
Return type: List
Example:
Map map = ["a": 1, "b": 2]
map.values(); // Returns: [1, 2]- map.each(
<Closure closure>): Iterates through dictionary data, passing key and value to the closure
Return type: List
Example:
Map map = ["a": 1, "b": 2]
map.each {String key,value ->
log.info(key)
log.info(value)
}