Skip to content
Reference > Classes

Map

Definition

Map var = .... ;

Extends

Extended By

Description

A mutable collection of unique keys which are mapped to values. Attempts to add duplicates will override existing key-value pairs. Entries are stored in the order inserted (Backed by Java LinkedHashMap)

Method Summary

Owner Name Return Type Description
Map constructor(key_values) Object Initialize map
Map clear() Boolean Removes all the mappings from this map. Returns "true" if the map is changed as a result.
Map containsKey(key) Boolean Returns true if this map contains the specified key.
Map containsValue(value) Boolean Returns true if this map contains the specified value.
Map get(key) Object Returns the value associated with the given key. Returns null if key does not exist.
Object getClassName() String Returns the string name of this object's class type.
Map getKeys() Set Returns the keys as a set.
Map getValues() List Returns the values as a list.
Map jsonPath(jsonPath) Object Checks the mapped path of the string supllied and returns the object at the end of the path if it exists. Returns itself if path is null. Use dot(.) to delimit path. Use numbers to traverse list elements and keys to traverse Maps.Alternatively, use Python-like syntax to access map element.
Map put(key, value) Object Adds the specified key value pair to this map and returns old value associated with the key. Returns null if key does not exist.
Map putAll(m) Object Copies all the mappings of the target map to this map.
Map remove(key) Object Removes the given key and returns the value associated with the key. Returns null if the key does not exist.
Map size() Integer Returns the number of key/value pairs in this map.
Object toJson() String Returns a string of a json representation of this object.

Method Definitions


constructor(key_values)

Map map = new Map(Object ... key_values)

Description

Initialize map

Parameter Definition

Name Type Description
key_values Object ...  initialize list with arguments

Example 1

1
2
3
Map m = new Map("dog", "Crouton", "cat", new list("Artemis","Reggie"));

// m = {dog=Crouton, cat=[Artemis, Reggie]}

Example 2

1
2
3
Map py_map = {"dog":"Crouton", "cat":["Artemis","Reggie"]};

// py_map = {dog=Crouton, cat=[Artemis, Reggie]}

Python-style syntax for initiating maps in AMI.


clear()

Boolean Map::clear()

Description

Removes all the mappings from this map. Returns "true" if the map is changed as a result.

Example 1

1
2
3
4
Map m = new Map("dog","Crouton","cat","Artemis","fish","Tofu");
m.clear();

// m = {}

containsKey(key)

Boolean Map::containsKey(Object key)

Description

Returns true if this map contains the specified key.

Parameter Definition

Name Type Description
key Object  key to check

Example 1

1
2
3
4
5
6
Map m = new Map("dog","Crouton","cat","Artemis","fish","Tofu");
m.containsKey("cat");
m.containsKey("bird");

// m.containsKey("cat") = true
// m.containsKey("bird") = false

containsValue(value)

Boolean Map::containsValue(Object value)

Description

Returns true if this map contains the specified value.

Parameter Definition

Name Type Description
value Object  value to check

get(key)

Object Map::get(Object key)

Description

Returns the value associated with the given key. Returns null if key does not exist.

Parameter Definition

Name Type Description
key Object  key of entry to get

Example 1

1
2
3
4
Map m = new Map("dog","Crouton", "cat",new list("Artemis","Reggie"));
m.get("cat");

// m.get("cat") = [Artemis, Reggie]

Example 2

1
2
3
4
Map py_map = {"dog":"Crouton", "cat":["Artemis","Reggie"]};
py_map["cat"][0];

// py_map["cat"][2] = null

Python-style syntax for retrieving nested elements in maps. Alternatively, use jsonPath() instead.


getClassName()

String Object::getClassName()

Description

Returns the string name of this object's class type.

Example 10

1
2
3
4
Map m = new Map("dog","Crouton","cat","Artemis","fish","Tofu");
m.getClassName();

// m.getClassName() = Map

getKeys()

Set Map::getKeys()

Description

Returns the keys as a set.

Example 1

1
2
3
4
Map m = new Map("dog","Crouton","cat","Artemis","fish","Tofu");
m.getKeys();

// m.getKeys() = [dog, cat, fish]

getValues()

List Map::getValues()

Description

Returns the values as a list.

Example 1

1
2
3
4
Map m = new Map("dog","Crouton","cat","Artemis","fish","Tofu");
m.getValues();

// m.getValues() = [Crouton, Artemis, Tofu]

jsonPath(jsonPath)

Object Map::jsonPath(String jsonPath)

Description

Checks the mapped path of the string supllied and returns the object at the end of the path if it exists. Returns itself if path is null. Use dot(.) to delimit path. Use numbers to traverse list elements and keys to traverse Maps.Alternatively, use Python-like syntax to access map element.

Parameter Definition

Name Type Description
jsonPath String  dot(.) delimited path

Example 1

1
2
3
4
5
6
7
Map m = new Map("dogs",new list("Crouton"), "cats",new List("Artemis","Reggie"));
String cats = m.jsonPath("cats");
String firstCat = m.jsonPath("cats.0");

// m.jsonPath("cats") = [Artemis, Reggie]
// cats = ["Artemis","Reggie"]
// firstCat = Artemis

The Map "m" is a map of lists mapping a pet type to the list of pets corresponding to that type. In this example, jsonPath() is used to extract the corresponding value (name of pets) of the supplied key, the list "cats".

Example 2

1
2
3
4
Map m = new Map("dogs",new list("Crouton"), "cats",new List("Artemis","Reggie"));
String firstCat = m["cats"][0];

// m["cats"][0] = Artemis

You can access nested map elements using Python-like syntax instead by supplying each key/index in a series of square brackets.

Example 3

1
2
3
4
5
6
Map m = new Map("dogs",new list("Crouton"), "cats",new List("Artemis","Reggie"));
String q1 = m.jsonPath(null);
String q2 = m.jsonPath("");

// m.jsonPath(null) = {dogs=[Crouton], cats=[Artemis, Reggie]}
// m.jsonPath("") = null

put(key,value)

Object Map::put(Object key, Object value)

Description

Adds the specified key value pair to this map and returns old value associated with the key. Returns null if key does not exist.

Parameter Definition

Name Type Description
key Object  key of entry
value Object  value of entry

Example 1

1
2
3
4
Map m = new Map("dog", "Crouton");
m.put("cat","Artemis");

// m = {dog=Crouton, cat=Artemis}

putAll(m)

Object Map::putAll(Map m)

Description

Copies all the mappings of the target map to this map.

Parameter Definition

Name Type Description
m Map  map to add

Example 1

1
2
3
4
5
Map m = new Map("dog", "Crouton");
Map m2 = new Map("cat","Artemis","fish","Tofu");
m.putAll(m2);

// m = {dog=Crouton, cat=Artemis, fish=Tofu}

remove(key)

Object Map::remove(Object key)

Description

Removes the given key and returns the value associated with the key. Returns null if the key does not exist.

Parameter Definition

Name Type Description
key Object  key of entry to remove

Example 1

1
2
3
4
Map m = new Map("dog","Crouton","cat","Artemis","fish","Tofu");
m.remove("fish");

// m = {dog=Crouton, cat=Artemis}

size()

Integer Map::size()

Description

Returns the number of key/value pairs in this map.

Example 1

1
2
3
4
Map m = new Map("dog","Crouton","cat","Artemis","fish","Tofu");
m.size();

// m.size() = 3

toJson()

String Object::toJson()

Description

Returns a string of a json representation of this object.

Example 10

1
2
3
4
Map m = new Map("dog","Crouton","cat","Artemis","fish","Tofu");
m.toJson();

// m.toJson() = {"dog":"Crouton","cat":"Artemis","fish":"Tofu"}