Map - collection type, encapsulated with [] when used, intermediate data is separated by , and exists in the form of key-value pairs
Define Map: Map map = ["<key1>":<value1>,"<key2>":<value2>]
example:
> Map map = ["a":1, "b": 2, "c":3]
Methods of the Map type:
- map.keys(): Get all the attribute names of the dictionary
Return value 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 value type: BigDecimal
example:
> Map map = ["a": 1, "b": 2]
> result = map.size() // returns: 2
- map.isEmpty(): Determine whether the dictionary is empty. Returns boolean - true if key-value map is not included, false if key-value map is included
Return value type: Boolean
example:
> Map map = ["a": 1, "b": 2]
> result = map.isEmpty() // returns: false
- map.remove(<String key>): Remove and return the element with the specified key
Return value type: Object
example:
> Map map = ["a": 1, "b": 2]
> map.remove("a") // returns: 1
- map.clear(): Remove all key-value pairs from the dictionary
Return value type: no return value
example:
> Map map = ["a": 1, "b": 2]
> map.clear()
- map.put(<String key>,<Object value>): store key-value pairs
Return value type: no return value
example:
> Map map = ["a": 1, "b": 2]
> map. put('c', 3)
- map.putIfAbsent(<String key>,<Object value>): store key-value pairs, if the key exists, it will not be modified under putIfAbsent
Return value type: Object
example:
> Map map = ["a": 1, "b": 2]
> map.putIfAbsent('a', 2) //The value of the key "a" is still 1 at this time
- map.containsKey(<String key>): whether to contain key
Return value type: Boolean
example:
> Map map = ["a": 1, "b": 2]
> map.containsKey("a"); // returns: true
- map.containsValue(<Object value>): whether to contain value
Return value type: Boolean
example:
> Map map = ["a": 1, "b": 2]
> map.containsValue(2); // returns: true
- map.values(): returns a collection of all values
Return value type: List
example:
> Map map = ["a": 1, "b": 2]
> map.values(); // returns: [1, 2]
- map.each(<Closure closure>): Traverse the data in the dictionary, pass in the key and value in the closure
Return value type: List
example:
> Map map = ["a": 1, "b": 2]
> map. each {String key, value ->
> log. info(key)
> log. info(value)
> }