Map (Foundation class)

From Axaptapedia

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 alway sorted on the key value.

Contents

How to use

Define

Map m = new Map(Types::STRING, Types::INTEGER);

Insert a value

m.insert("Wassini", 37);
m.insert("Eric", 102);

Exists value

To see if a value already is added, use the exists method:

if (m.exists("Wassini"))
   print "Yes!";
else
   print "No!";

Getting values

There are several ways to get the values in the map.

  1. Using a MapIterator
  2. Using a direct method

MapIterator

The MapIterator loops throug the complete map:

MapIterator mi;
 
mi = new MapIterator(m);
 
while (mi.more())
{
  print mi.key();
  print mi.value();
 
  mi.next();
}


Direct method

It is possible to get find a specific value from the key:

print m.lookup("Wassini");

Removing values

Just use the remove method to remove the active key/value pair.

m.remove("Wassini");

Updating values

It is not possible to update a value directly:

int age;
str keyid = "Wassini";
 
age = m.lookup(keyid);
m.remove(keyid);
m.insert(keyid, age + 1);


Other methods

// Get number of elements:
print m.elements(); 
 
// Get the used types:
print m.definitionString(); 
 
// Dump the whole map as a string:
print m.toString();

See also