Skip to main content

MapUtil

MapUtil provides utility methods for creating and working with HashMap<Object, Object> — a key-value data structure that associates unique keys with values. Maps are especially useful for defining rows in a SuiteTable.

Create a Map

Use MapUtil.create() to create a new empty map, or define one inline using map literal syntax.

// Using MapUtil
var map = MapUtil.create();
MapUtil.add(map, "name", "Alice");

// Using literal syntax
var map = {
"name": "Alice",
"age": 30
};

// Empty map literal
var empty = {:};

Methods

MapUtil.create(): Map

Creates and returns a new empty map.

@returns — A new empty HashMap<Object, Object>.


MapUtil.add(map: Map, key: String, value: Object): void

Adds or updates a key-value pair in the map.

@param map — The target map.

@param key — The key to add or update.

@param value — The value to associate with the key.

MapUtil.add(map, "country", "USA");

size(): Integer

Returns the number of key-value pairs in the map.

@returns — The current size of the map.

var count = map.size();

isEmpty(): Boolean

Returns true if the map contains no entries.

if (map.isEmpty()) { ... }

containsKey(key: Object): Boolean

Returns true if the map contains the specified key.

@param key — The key to check.

map.containsKey("name"); // true

containsValue(value: Object): Boolean

Returns true if the map contains the specified value.

@param value — The value to check.

map.containsValue("Alice"); // true

remove(key: String): Object

Removes the entry with the specified key and returns its value.

@param key — The key to remove.

@returns — The value that was associated with the key.

var removed = map.remove("age"); // returns 30

keySet(): Set

Returns the set of all keys in the map.

map.keySet(); // e.g. name, age, country

values(): Collection

Returns all values in the map.

map.values(); // e.g. Alice, 30, USA

Full Example

var map = MapUtil.create();
MapUtil.add(map, "name", "Steve");
MapUtil.add(map, "age", 40);
MapUtil.add(map, "birthday", LocalDate.now());

map.values(); // returns 2023-11-22,Steve,40
map.keySet(); // returns birthday,name,age
map.remove("age"); // removes "age" and returns 40

return map;