Axaptapedia is now maintained by AgileCadence For more information please click here.
Map Class
Map (Foundation class) consist of a data set that contains a key and a corresponding value, where the key is unique. The key and the value need not be from the same data type.
A Map is always sorted on the key value.
Contents
How to use[edit]
Define[edit]
<xpp> Map m = new Map(Types::STRING, Types::INTEGER); </xpp> or <xpp> Map m = new Map(typeId2Type(typeid(recId)), Types::Record); </xpp>
Insert a value[edit]
<xpp> m.insert("Wassini", 37); m.insert("Eric", 102); </xpp>
Exists value[edit]
To see if a value already is added, use the exists method: <xpp> if (m.exists("Wassini"))
print "Yes!";
else
print "No!";
</xpp>
Lookup value[edit]
To get the value linked to a specific key use the lookup method: <xpp> Integer i;
i = m.lookup("Wassini")) </xpp> If key don't exist in map lookup function throw error so is better to test before is key exist
Getting values[edit]
There are several ways to get the values in the map.
- Using a MapIterator
- Using a MapEnumerator
- Using a direct method
MapIterator[edit]
The MapIterator loops throug the complete map:
<xpp> MapIterator mi;
mi = new MapIterator(m);
while (mi.more()) {
print mi.key(); print mi.value();
mi.next();
} </xpp>
MapEnumerator[edit]
MapEnumerator Class is like MapIterator Class, but does not allow the deletion of elements during enumeration. MapEnumerator Class automatically created on the same tier as the Map Class, and MapIterator does not. <xpp> MapEnumerator me = m.getEnumerator();
while (me.moveNext()) {
print me.currentKey(); print me.currentValue();
} </xpp>
Direct method[edit]
It is possible to get find a specific value from the key:
<xpp> print m.lookup("Wassini"); </xpp>
Removing values[edit]
Just use the remove method to remove the active key/value pair.
<xpp> m.remove("Wassini"); </xpp>
Updating values[edit]
It is not possible to update a value directly: <xpp> int age; str keyid = "Wassini";
age = m.exists(keyid) ? m.lookup(keyid) : 0; m.insert(keyid, age + 1); </xpp>
Other methods[edit]
<xpp> // Get number of elements: print m.elements();
// Get the used types: print m.definitionString();
// Dump the whole map as a string: print m.toString(); </xpp>
Passing across tiers[edit]
As with other foundation classes, the Map can be passed across tiers by converting it to a container. The pack method converts it to a container: <xpp> container packedMap = m.pack(); </xpp> To convert the packed container back to a Map, call the static create method of the Map class: <xpp> Map map = Map::create(packedMap); </xpp>