Skip to main content

Map Functions

Functions on MapEntry

getKey(MapEntry entry)

  • Returns the key of the map entry
def MapEntry<string, real> employeeToSalary = "John Doe" to 50000.0;
def string key = employeeToSalary.getKey(); // "John Doe"
def string key2 = getKey(employeeToSalary); // "John Doe"
def MapEntry<int, string> employeeIdToName = 1 to "John Doe";
def int key = employeeIdToName.getKey(); // 1
def int key2 = getKey(employeeIdToName); // 1

getValue(MapEntry entry)

  • Returns the value of the map entry
def MapEntry<string, real> employeeToSalary = "John Doe" to 50000.0;
def real value = employeeToSalary.getValue(); // 50000.0
def real value2 = getValue(employeeToSalary); // 50000.0
def MapEntry<int, string> employeeIdToName = 1 to "John Doe";
def string value = employeeIdToName.getValue(); // "John Doe"
def string value2 = getValue(employeeIdToName); // "John Doe"

setKey(MapEntry entry, KeyType key)

  • Sets the key of the map entry
def MapEntry<string, real> employeeToSalary = "John Doe" to 50000.0;
employeeToSalary.setKey("Jane Doe"); // employeeToSalary = Jane Doe -> 50000.0
setKey(employeeToSalary, "Jane Doe"); // employeeToSalary = Jane Doe -> 50000.0
def MapEntry<int, string> employeeIdToName = 1 to "John Doe";
employeeIdToName.setKey(2); // employeeIdToName = 2 -> "John Doe"
setKey(employeeIdToName, 2); // employeeIdToName = 2 -> "John Doe"

setValue(MapEntry entry, ValueType value)

  • Sets the value of the map entry
def MapEntry<string, real> employeeToSalary = "John Doe" to 50000.0;
employeeToSalary.setValue(75000.0); // employeeToSalary = John Doe -> 75000.0
setValue(employeeToSalary, 75000.0); // employeeToSalary = John Doe -> 75000.0
def MapEntry<int, string> employeeIdToName = 1 to "John Doe";
employeeIdToName.setValue("Jane Doe"); // employeeIdToName = 1 -> "Jane Doe"
setValue(employeeIdToName, "Jane Doe"); // employeeIdToName = 1 -> "Jane Doe"

Functions on Map

get(Map map, KeyType key)

  • Returns the value in the map for the given key
  • Returns null if value is not present in the map
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
def real value1 = employeeToSalary.get("John Doe"); // 50000.0
def real value2 = get(employeeToSalary, "Jack"); // null
def Map<int, string> employeeIdToName = mapOf(1 to "John Doe", 2 to "Jane Doe");
def string value1 = employeeIdToName.get(1); // "John Doe"
def string value2 = get(employeeIdToName, 3); // null

put(Map map, KeyType key, ValueType value)

  • Inserts the key in the map with the given value
  • If the key is already present in map, it gets replaced
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
employeeToSalary.put("Jack", 12000.0); // employeeToSalary = "John Doe" -> 50000.0, "Jane Doe" -> 75000.35, "Jack" -> 12000.0
put(employeeToSalary, "John Doe", 90000.0); // employeeToSalary = "John Doe" -> 90000.0, "Jane Doe" -> 75000.35, "Jack" -> 12000.0
def Map<int, string> employeeIdToName = mapOf(1 to "John Doe", 2 to "Jane Doe");
employeeIdToName.put(3, "Jack"); // employeeIdToName = 1 -> "John Doe", 2 -> "Jane Doe", 3 -> "Jack"
put(employeeIdToName, 3, "Jack"); // employeeIdToName = 1 -> "John Doe", 2 -> "Jane Doe", 3 -> "Jack"

getKeys(Map map)

  • Returns all the keys present in the map as a Set
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
def Set<string> keys1 = employeeToSalary.getKeys(); // ["John Doe", "Jane Doe"]
def Set<string> keys2 = getKeys(employeeToSalary); // ["John Doe", "Jane Doe"]
def Map<int, string> employeeIdToName = mapOf(1 to "John Doe", 2 to "Jane Doe");
def Set<int> keys1 = employeeIdToName.getKeys(); // [1, 2]
def Set<int> keys2 = getKeys(employeeIdToName); // [1, 2]

getValues(Map map)

  • Returns all the values present in the map as a List
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
def Set<real> values = employeeToSalary.getValues(); // [50000.0, 75000.35]
def Set<real> values2 = getValues(employeeToSalary); // [50000.0, 75000.35]
def Map<int, string> employeeIdToName = mapOf(1 to "John Doe", 2 to "Jane Doe");
def List<string> values1 = employeeIdToName.getValues(); // ["John Doe", "Jane Doe"]
def List<string> values2 = getValues(employeeIdToName); // ["John Doe", "Jane Doe"]

getEntries(Map map)

  • Returns all the entries present in the map as a Set<MapEntry>
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
def Set<MapEntry<string, real>> entries1 = employeeToSalary.getEntries(); // ["John Doe" -> 50000.0, "Jane Doe" -> 75000.35]
def Set<MapEntry<string, real>> entries2 = getEntries(employeeToSalary); // ["John Doe" -> 50000.0, "Jane Doe" -> 75000.35]
def Map<int, string> employeeIdToName = mapOf(1 to "John Doe", 2 to "Jane Doe");
def Set<MapEntry<int, string>> entries1 = employeeIdToName.getEntries(); // [1 -> "John Doe", 2 -> "Jane Doe"]
def Set<MapEntry<int, string>> entries2 = getEntries(employeeIdToName); // [1 -> "John Doe", 2 -> "Jane Doe"]

removeKey(Map map, KeyType key)

  • Removes the key, if it's present in the map
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
employeeToSalary.removeKey("John Doe"); // employeeToSalary = "Jane Doe" -> 75000.35
employeeToSalary.removeKey("Jack"); // employeeToSalary = "Jane Doe" -> 75000.35
removeKey(employeeToSalary, "John Doe"); // employeeToSalary = "Jane Doe" -> 75000.35
def Map<int, string> employeeIdToName = mapOf(1 to "John Doe", 2 to "Jane Doe");
employeeIdToName.removeKey(1); // employeeIdToName = 2 -> "Jane Doe"
employeeIdToName.removeKey(3); // employeeIdToName = 2 -> "Jane Doe"
removeKey(employeeIdToName, 2); // employeeIdToName = {}

clearMap(Map map)

  • Removes all the entries from the map
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
employeeToSalary.clearMap(); // employeeToSalary = {}
clearMap(employeeToSalary); // employeeToSalary = {}
def Map<int, string> employeeIdToName = mapOf(1 to "John Doe", 2 to "Jane Doe");
employeeIdToName.clearMap(); // employeeIdToName = {}
clearMap(employeeIdToName); // employeeIdToName = {}

hasKey(Map map, KeyType key)

  • Returns a boolean indicating whether a key is present in the map
  • You can use the not variant of this function as detailed here
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
def boolean value1 = employeeToSalary.hasKey("John Doe"); // true
def boolean value2 = employeeToSalary.hasKey("Jack"); // false
def boolean value3 = hasKey(employeeToSalary, "John Doe"); // true
def boolean value4 = hasKey(employeeToSalary, "Jack"); // false
def Map<int, string> employeeIdToName = mapOf(1 to "John Doe", 2 to "Jane Doe");
def boolean value1 = employeeIdToName.hasKey(1); // true
def boolean value2 = employeeIdToName.hasKey(3); // false
def boolean value3 = hasKey(employeeIdToName, 1); // true
def boolean value4 = hasKey(employeeIdToName, 3); // false

hasValue(Map map, ValueType value)

  • Returns a boolean indicating whether a value is present in the map
  • You can use the not variant of this function as detailed here
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
def boolean value1 = employeeToSalary.hasValue(50000.0); // true
def boolean value2 = hasValue(employeeToSalary, 50000.0); // true
def boolean value3 = employeeToSalary.hasValue(100000.0); // false
def boolean value4 = hasValue(employeeToSalary, 100000.0); // false

hasUniqueValues(Map map)

  • Returns a boolean indicating whether all values present in the map are unique
  • You can use the not variant of this function as detailed here
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
def boolean value1 = employeeToSalary.hasUniqueValues(); // true
def boolean value2 = hasUniqueValues(employeeToSalary); // true
def Map<string, string> employerToDepartment = mapOf("John" to "HR", "Jane" to "HR", "Jack" to "Sales");
def boolean value3 = employerToDepartment.hasUniqueValues(); // false
def boolean value4 = hasUniqueValues(employerToDepartment); // false

hasAllKeys(Map map, List<KeyType> keys)

  • Returns true only if all the given keys are present in the map, returns false otherwise
  • You can use the not variant of this function as detailed here
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
def boolean value1 = employeeToSalary.hasAllKeys(["John Doe", "Jane Doe"]); // true
def boolean value2 = employeeToSalary.hasAllKeys(["Jane Doe", "Jack"]); // false
def boolean value3 = hasAllKeys(employeeToSalary, ["John Doe", "Jane Doe"]); // true
def boolean value4 = hasAllKeys(employeeToSalary, ["Jane Doe", "Jack"]); // false
def Map<int, string> employeeIdToName = mapOf(1 to "John Doe", 2 to "Jane Doe");
def boolean value1 = employeeIdToName.hasAllKeys([1, 2]); // true
def boolean value2 = employeeIdToName.hasAllKeys([1, 3]); // false
def boolean value3 = hasAllKeys(employeeIdToName, [1, 2]); // true
def boolean value4 = hasAllKeys(employeeIdToName, [1, 3]); // false

putIfAbsent(Map map, KeyType key, ValueType value)

  • Inserts the key in the map with the given value, only if the key is not already present in the map
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
employeeToSalary.putIfAbsent("Jack", 12000.0); // employeeToSalary = "John Doe" -> 50000.0, "Jane Doe" -> 75000.35, "Jack" -> 12000.0
employeeToSalary.putIfAbsent("John Doe" to 90000.0); // employeeToSalary = "John Doe" -> 50000.0, "Jane Doe" -> 75000.35, "Jack" -> 12000.0
putIfAbsent(employeeToSalary, "Jack", 12000.0); // employeeToSalary = "John Doe" -> 50000.0, "Jane Doe" -> 75000.35, "Jack" -> 12000.0
putIfAbsent(employeeToSalary, "John Doe", 90000.0); // employeeToSalary = "John Doe" -> 50000.0, "Jane Doe" -> 75000.35, "Jack" -> 12000.0
def Map<int, string> employeeIdToName = mapOf(1 to "John Doe", 2 to "Jane Doe");
employeeIdToName.putIfAbsent(3, "Jack"); // employeeIdToName = 1 -> "John Doe", 2 -> "Jane Doe", 3 -> "Jack"
employeeIdToName.putIfAbsent(1, "John Doe"); // employeeIdToName = 1 -> "John Doe", 2 -> "Jane Doe", 3 -> "Jack"
putIfAbsent(employeeIdToName, 3, "Jack"); // employeeIdToName = 1 -> "John Doe", 2 -> "Jane Doe", 3 -> "Jack"
putIfAbsent(employeeIdToName, 1, "John Doe"); // employeeIdToName = 1 -> "John Doe", 2 -> "Jane Doe", 3 -> "Jack"

putAll(Map map, List<MapEntry<KeyType, ValueType>> entries)

  • Inserts all the entries in the map
  • If any key is already present, it gets replaced
def Map<string, real> employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
employeeToSalary.putAll(["Jack" to 12000.0, "John Doe" to 90000.0]); // employeeToSalary = "John Doe" -> 90000.0, "Jane Doe" -> 75000.35, "Jack" -> 12000.0
putAll(employeeToSalary, ["Jack" to 12000.0, "John Doe" to 90000.0]); // employeeToSalary = "John Doe" -> 90000.0, "Jane Doe" -> 75000.35, "Jack" -> 12000.0
def Map<int, string> employeeIdToName = mapOf(1 to "John Doe", 2 to "Jane Doe");
employeeIdToName.putAll([3 to "Jack", 1 to "John Doe"]); // employeeIdToName = 1 -> "John Doe", 2 -> "Jane Doe", 3 -> "Jack"
putAll(employeeIdToName, [3 to "Jack", 1 to "John Doe"]); // employeeIdToName = 1 -> "John Doe", 2 -> "Jane Doe", 3 -> "Jack"