Class Map
A map is a collection of key-value pairs. Both the key and the value are strings.
The elements in a map are automatically sorted on their keys. The Map class supports two constructors. The default constructor accepts no parameters and initializes a Map with an empty key-value pair collection. The other constructor accepts a String.Examples
Map m = Map("height=25\nwidth=10");
m.first();
while (!m.eof()){
printLine(m.getKey() + " = " + m.getVal());
m.next();
}
Constructors
Map()
The default constructor. Called with no parameters, it creates an empty Map.
Declaration
Map Map()
Type | Description |
Map | The new Map with an empty key-value pair collection. |
Map(String)
Pass a String containing key-value pairs separated by "\n" like this: "key=value\nkey=value\nkey=value,..."
Declaration
Map Map(String value)
Examples
Map m = Map("roses = red\nviolets = blue");
Parameters
Type | Name | Description |
String | value | A String containing key-value pairs. |
Type | Description |
Map | The new Map populated with a collection of key-value pairs. |
Methods
insert(String, String)
Adds a new key-value pair to the map.
Declaration
Map insert(String key, String value)
Examples
Map m;
m.insert("Super", "Office");
Parameters
Type | Name | Description |
String | key | The key. |
String | value | The value belonging to the key. |
Returns
Type | Description |
Map | A reference to itself. |
exists(String)
Checks if the map contains the given key.
Declaration
Bool exists(String key)
Examples
Map m = Map("height = 25\nwidth = 10\ndepth = 7");
String key = "height";
printLine(m.exists(key).toString());
Parameters
Type | Name | Description |
String | key | The key to search for. |
Returns
Type | Description |
Bool | True if the key exists in the map. |
size()
Counts the elements in the map and returns that number.
Declaration
Integer size()
Examples
Map m = Map("height=25\nwidth=10\ndepth=7");
printLine(m.size().toString());
Returns
Type | Description |
Integer | The number of elements in the map. |
get(String)
Returns the value for the given key.
Declaration
String get(String key)
Examples
Map m = Map("height=25\nwidth=10");
String key = depth;
printLine(m.get(key));
Parameters
Type | Name | Description |
String | key | The key to look up a value for. |
Returns
Type | Description |
String | The value for the given key. |
getKey()
Returns the key pointed to by the map iterator.
Declaration
String getKey()
Examples
Map m = Map("height=25\nwidth=10");
printLine(m.getKey());
Returns
Type | Description |
String | The key pointed to by the internal iterator. |
getVal()
Returns the value pointed to by the map iterator.
Declaration
String getVal()
Examples
Map m = Map("height=25\nwidth=10");
printLine(m.getVal());
Returns
Type | Description |
String | The value pointed to by the internal iterator. |
getWithFallback(String, String)
Returns the fallback value if key does not exist.
Declaration
String getWithFallback(String key, String fallback)
Examples
Map m = Map("height=25\nwidth=10");
printLine(m.getWithFallback("foo", "bar"));
Parameters
Type | Name | Description |
String | key | The key to look up. |
String | fallback | The fallback value. |
Returns
Type | Description |
String | The value of key, or fallback value if key does not exist. |
increaseValueForKey(String, Integer)
When working with numeric strings, you can increment values stored in the map. Provide the key to look up the element and the value to add to the currently stored value.
Declaration
Void increaseValueForKey(String key, Integer value)
Examples
Map m = Map("height=25\nwidth=10");
m.increaseValueForKey("height", -5);
Parameters
Type | Name | Description |
String | key | The key to look up. |
Integer | value | The amount to add to the current value. |
Returns
Type | Description |
Void |
increaseValueForKey(String, Float)
When working with numeric strings, you can increment values stored in the map. Provide the key to look up the element and the value to add to the currently stored value.
Declaration
Void increaseValueForKey(String key, Float value)
Examples
Map m = Map("height=25\nwidth=10");
m.increaseValueForKey("height", 2.5);
Parameters
Type | Name | Description |
String | key | The key to look up. |
value | The amount to add to the current value. |
Returns
Type | Description |
Void |
remove(String)
Removes the element with the given key.
Declaration
Void remove(String key)
Examples
Map m = Map("roses = red\nviolets = blue");
m.remove("violets");
Parameters
Type | Name | Description |
String | key | The key matching the element to remove to remove. |
Returns
Type | Description |
Void |
clear()
Removes all elements from the map.
Declaration
Void clear()
Examples
Map m = Map("roses = red\nviolets = blue");
m.clear();
Returns
Type | Description |
Void |
eof()
Returns true if the map iterator has moved past the end of the map, otherwise false.
Declaration
Bool eof()
Examples
Map m;
if (m.eof()) {
printLine("You have reached the final frontier");
}
Returns
Type | Description |
Bool | True if the internal iterator is past the end of the map, otherwise False. |
first()
Rewinds the internal iterator to the 1st element. Returns false if the map is empty.
Declaration
Bool first()
Examples
Map m = Map("height=25\nwidth=10");
m.first();
Returns
Type | Description |
Bool | True if map is not empty, otherwise false. |
next()
Moves the map iterator to next position. Returns false if eof().
Declaration
Bool next()
Examples
Map m = Map("height=25\nwidth=10");
m.next();
Returns
Type | Description |
Bool | False if eof(), otherwise true. |
toJson()
Converts the Map to JSON.
Declaration
String toJson()
Examples
Map m = Map("height=25\nwidth=10");
printLine(m.toJson());
Returns
Type | Description |
String | The Map represented as JSON string. |
fromJson(String)
Converts a JSON string to a map. Format: {"key": "value", "foo": "bar"}
Declaration
Void fromJson(String json)
Examples
String s = "{"depth":"7","height":"20","width":"12.500000"}";
Map m;
m.fromJson(s);
Parameters
Type | Name | Description |
String | json | The Json-formatted string of key-value pairs to add. |
Returns
Type | Description |
Void |