and value can be of any valid X++ type, including objects. The types of the key
and value are specified in the declaration of the map. The way in which maps are
implemented means that access to the values is very
Multiple keys can map to the same value, but one key can map to only one value
at a time. If a [key, value] pair is added where the key already exists, it will
replace the existing pair with that key value.
The following methods are commonly used on Map objects:
• insert(_key, _value) - inserts a member (pair) into the map, where
_key and _value can be anytype.
• remove(_key) - removes a [key, value] pair from a map.
• exists(_key) - determines whether a particular value exists as a key
in the map.
• lookup(_key) - returns the value mapped to by a particular key
value.
• elements() - returns the number of members contained in the map.
• getEnumerator() - returns a MapEnumerator object for this Map
object.
The MapEnumerator class allows you to traverse through the members of a
map. The following methods are commonly used on MapEnumerator objects:
• currentKey() - retrieves the key of the pair currently pointed to in
the map.
• currentValue() - retrieves the value of the pair currently pointed to
in the map.
• moveNext() - moves the enumerator to the next pair in the map. Map
enumerators start before the first pair in the map, so moveNext()
must be called to make it point to the first pair in the map.
• reset() - moves the enumerator to the start of the map.
The following example demonstrates how to create a map of Customers per
State, then move through it and extract values from it.
1. Create a new Map object, specifying the data type it will contain.
2. Loop through the records in the CustTable. For each, useMap.exists() to check if CustTable.stateName() already exists as a
key in the map.
3. If it does not already exist, use Map.insert() to insert a pair of(CustTable.stateName(), 1).
4. If it does already exist, use Map.insert() to re-insert the pair with the
same key (which will overwrite the existing pair), but the new value
equals the existing pair value plus 1.
5. Set the MapEnumerator object using the Map.getEnumerator()method.
6. Go to the start of the map, using MapEnumerator.reset().
7. Move through the members of the map, usingMapEnumerator.moveNext().
8. Pull the value of the current member in the map, usingMapEnumerator.currentKey() andMapEnumerator.currentValue().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | Map mapStateNumbers; MapEnumerator enumerator; CustTable custTable; mapStateNumbers = new Map(Types::String, Types::Integer); while select custTable { if(mapStateNumbers.exists(custTable.stateName())) { mapStateNumbers.insert(custTable.stateName(), mapStateNumbers.lookup(custTable.stateName())+1); } else { mapStateNumbers.insert(custTable.StateName(), 1); } } enumerator = mapStateNumbers.getEnumerator(); while (enumerator.moveNext()) { info(strfmt("%1 customers are located in %2.", enumerator.currentValue(), enumerator.currentKey()); } |
Best Regards,
Hossein Karimi
No comments:
Post a Comment