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 [hide]
1 How to use
1.1 Define
1.2 Insert a value
1.3 Exists value
1.4 Getting values
1.4.1 MapIterator
1.4.2 MapEnumerator
1.4.3 Direct method
1.5 Removing values
1.6 Updating values
1.7 Other methods
2 See also
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.
Using a MapIterator
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();
} MapEnumerator
MapEnumerator class is like MapIterator class, but allows the deletion of elements during enumeration and MapIterator does not.
MapEnumerator me = m.getEnumerator();
while (me.moveNext())
{
print me.currentKey();
print me.currentValue();
} 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.exists(keyid) ? m.lookup(keyid) : 0;
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();
Sunday, December 21, 2008
Subscribe to:
Post Comments (Atom)
How to identify the user that was used to change an object from AOT in AX2012
Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...
-
Combo boxes in Dialogs are added by adding a DialogField object with the type of an enum. The enum is then controlling what items you can pi...
-
This article explains: How to picking list through a job using X++ in Dynamics AX. Applied on: Dynamics AX 2009 SP1 Create a job a...
-
A while ago I had the need to translate labels, I was creating eMail bodys while using SysMailer and wanted to use different languages for d...
1 comment:
Map is a very useful class which basically keeps objects which the types of them determined in initialization. Map keeps two variables for each object, key and value.
Map map = new Map(Types::String, Types::Integer);
MapEnumerator mapEnumerator;
Set set;
container con;
;
map.insert('paper', 35); // Map = ['paper' -> 35]
map.insert('cup', 1500); // Map = ['cup' -> 1500, 'paper' -> 35] (It will automatically put ascending order according to keys)
map.insert('paper', 75); // Map = ['cup' -> 1500, 'paper' -> 75] (It will automatically replaces value, if key already exists in the map)
map.insert('knife', 3.4); // Map = ['cup' -> 1500, 'knife' -> 3, 'paper' -> 75] (It will automatically work to convert key/value to the type which is determined in initialization (if it could not convert, it will throw error))
print map.elements(); // prints count of elements in map which is 3
print map.exists('cup'); // prints "true" which means key 'cup' exists in map
print map.empty(); // prints "false" which means map is not empty (ie. there is at least one element in it)
set = map.keySet(); // fills set with the keys of map ['cup', 'knife', 'paper']
print map.keyType(); // prints "String" which is the type of key of map
print map.lookup('paper'); // prints 75 which is the value of the key 'paper'
con = map.pack(); // map keys and values are packed into container
map = null; // resets the map
map = map::create(con); // map is recovered from packed container
//print map.lookup('pencil'); // throws error 'The value "pencil" is not found in the map.'. So lookup method must be called after checking if key exists in the map
mapEnumerator = map.getEnumerator(); // enumerator is initialized with map
while (mapEnumerator.moveNext()) // statement will loop until mapEnumerator reaches end of map
{
print mapEnumerator.currentKey(); // prints current key
print mapEnumerator.current(); // prints current key (same as calling currentKey)
print mapEnumerator.currentValue(); // prints current value
}
pause;
Post a Comment