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.Syntax
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()
Returns
Type | Description |
---|---|
Map | The new Map with an empty key-value pair collection. |
Examples
Map m = Map("height=25\nwidth=10");
m.first();
while (!m.eof()){
printLine(m.getKey() + " = " + m.getVal());
m.next();
}
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)
Parameters
Type | Name | Description |
---|---|---|
String | value | A String containing key-value pairs. |
Returns
Type | Description |
---|---|
Map | The new Map populated with a collection of key-value pairs. |
Remarks
Don't add space between \n and the 1st character of the key!
Examples
Map m = Map("roses = red\nviolets = blue");
Methods
clear()
Removes all elements from the map.
Declaration
Void clear()
Returns
Type | Description |
---|---|
Void |
Examples
Map m = Map("roses = red\nviolets = blue");
m.clear();
eof()
Returns true if the map iterator has moved past the end of the map, otherwise false.
Declaration
Bool eof()
Returns
Type | Description |
---|---|
Bool | True if the internal iterator is past the end of the map, otherwise False. |
Remarks
That eof() returns true is not the same as the map is empty. It can be, but it doesn't have to be. Use size() to check if the map is truly empty.
Examples
Map m;
if (m.eof()) {
printLine("You have reached the final frontier");
}
exists(String)
Checks if the map contains the given key.
Declaration
Bool exists(String key)
Parameters
Type | Name | Description |
---|---|---|
String | key | The key to search for. |
Returns
Type | Description |
---|---|
Bool | True if the key exists in the map. |
Examples
Map m = Map("height = 25\nwidth = 10\ndepth = 7");
String key = "height";
printLine(m.exists(key).toString());
first()
Rewinds the internal iterator to the 1st element. Returns false if the map is empty.
Declaration
Bool first()
Returns
Type | Description |
---|---|
Bool | True if map is not empty, otherwise false. |
Examples
Map m = Map("height=25\nwidth=10");
m.first();
fromJson(String)
Converts a JSON string to a map. Format: {"key": "value", "foo": "bar"}
Declaration
Void fromJson(String json)
Parameters
Type | Name | Description |
---|---|---|
String | json | The Json-formatted string of key-value pairs to add. |
Returns
Type | Description |
---|---|
Void |
Examples
String s = "{"depth":"7","height":"20","width":"12.500000"}";
Map m;
m.fromJson(s);
get(String)
Returns the value for the given key.
Declaration
String get(String 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. |
Examples
Map m = Map("height=25\nwidth=10");
String key = depth;
printLine(m.get(key));
getKey()
Returns the key pointed to by the map iterator.
Declaration
String getKey()
Returns
Type | Description |
---|---|
String | The key pointed to by the internal iterator. |
Examples
Map m = Map("height=25\nwidth=10");
printLine(m.getKey());
getVal()
Returns the value pointed to by the map iterator.
Declaration
String getVal()
Returns
Type | Description |
---|---|
String | The value pointed to by the internal iterator. |
Examples
Map m = Map("height=25\nwidth=10");
printLine(m.getVal());
getWithFallback(String, String)
Returns the fallback value if key does not exist.
Declaration
String getWithFallback(String key, String fallback)
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. |
Examples
Map m = Map("height=25\nwidth=10");
printLine(m.getWithFallback("foo", "bar"));
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)
Parameters
Type | Name | Description |
---|---|---|
String | key | The key to look up. |
value | The amount to add to the current value. |
Returns
Type | Description |
---|---|
Void |
Remarks
You can pass the increment as either Integer or Float.
Examples
Map m = Map("height=25\nwidth=10");
m.increaseValueForKey("height", 2.5);
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)
Parameters
Type | Name | Description |
---|---|---|
String | key | The key to look up. |
Float | value | The amount to add to the current value. |
Returns
Type | Description |
---|---|
Void |
Remarks
You can pass the increment as either Integer or Float.
Examples
Map m = Map("height=25\nwidth=10");
m.increaseValueForKey("height", -5);
insert(String, String)
Adds a new key-value pair to the map.
Declaration
Map insert(String key, String value)
Parameters
Type | Name | Description |
---|---|---|
String | key | The key. |
String | value | The value belonging to the key. |
Returns
Type | Description |
---|---|
Map | A reference to itself. |
Remarks
This function will reset the internal iterator in the map.
Examples
Map m;
m.insert("Super", "Office");
next()
Moves the map iterator to next position. Returns false if eof().
Declaration
Bool next()
Returns
Type | Description |
---|---|
Bool | False if eof(), otherwise true. |
Examples
Map m = Map("height=25\nwidth=10");
m.next();
remove(String)
Removes the element with the given key.
Declaration
Void remove(String key)
Parameters
Type | Name | Description |
---|---|---|
String | key | The key matching the element to remove to remove. |
Returns
Type | Description |
---|---|
Void |
Examples
Map m = Map("roses = red\nviolets = blue");
m.remove("violets");
size()
Counts the elements in the map and returns that number.
Declaration
Integer size()
Returns
Type | Description |
---|---|
Float | The number of elements in the map. |
Remarks
An empty map has size == 0.
Examples
Map m = Map("height=25\nwidth=10\ndepth=7");
printLine(m.size().toString());
toJson()
Converts the Map to JSON.
Declaration
String toJson()
Returns
Type | Description |
---|---|
String | The Map represented as JSON string. |
Examples
Map m = Map("height=25\nwidth=10");
printLine(m.toJson());