Map Class
The Map class allows you to associate one value (the key) with another value. Both the key and value can be any valid X++ type, including objects. The types of the key and the value are specified in the declaration of the map. The way in which maps are implemented means that access to the values is very fast.
Syntax
class Map extends Object
Run On
Called
Methods
Method | Description | |
---|---|---|
cancelTimeOut | Cancels a previous method call to the setTimeOut method. (Inherited from Object.) | |
definitionString | Returns a string that contains the definition of the map. | |
domainSet | Creates a set of the key (domain) values in a map. | |
domainType | Determines the type of the key (domain) values in a map. | |
elements | Returns the number of elements in the map. | |
empty | Determines whether the map contains any (key, value) pairs. | |
equal | Determines whether the specified object is equal to the current one. (Inherited from Object.) | |
exists | Determines whether a particular value exists as a key in the map. | |
getEnumerator | Creates an enumerator for the map which allows you to traverse the map. | |
getTimeOutTimerHandle | Returns the timer handle for the object. (Inherited from Object.) | |
handle | Retrieves the handle of the class of the object. (Inherited from Object.) | |
insert | Inserts an element (keyValue, valueValue pair) in the map. | |
keySet | Returns a set that contains the key values from a map. | |
keyType | Returns the type of the key values in a map. | |
lookup | Returns the value mapped to by a particular key value. | |
new | Creates a new map. (Overrides the new Method.) | |
notify | Releases the hold on an object that has called the wait method on this object. (Inherited from Object.) | |
notifyAll | Releases a lock on the object that was issued by the wait method on this object. (Inherited from Object.) | |
objectOnServer | Determines whether the object is on a server. (Inherited from Object.) | |
owner | Returns the instance that owns the object. (Inherited from Object.) | |
pack | Serializes the current instance of the Map class. | |
rangeSet | Returns a set that contains the values (ranges) mapped to by the keys in a map. | |
rangeType | Determines the type of the values (ranges) mapped to by the keys in a map. | |
remove | Removes a (key, value) pair from a map. | |
setTimeOut | Sets up the scheduled execution of a specified method. (Inherited from Object.) | |
toString | Returns a description of the (key, value) pairs in the map. (Overrides the toString Method.) | |
usageCount | Returns the current number of references, that is, the value of the reference counter, that the object has. (Inherited from Object.) | |
valueSet | Returns a set that contains the values mapped to by the keys in a map. | |
valueType | Returns the type of the values that are mapped to by the keys in a map. | |
wait | Pauses a process. (Inherited from Object.) | |
xml | Returns an XML string that represents the current object. (Overrides the xml Method.) | |
::create | Creates a map from the container that was obtained from a prior call to the Map.pack method. | |
::createFromXML | ||
::equal | Determines whether two maps are equal. |
Top
Remarks
Multiple keys can map to the same value, but one key can map to only one value at a time. If you add a (key, value) pair with an existing key value, it will replace the existing pair with that key value.
The (key, value) pairs in a map can be traversed using the MapEnumerator class.
Examples
The following example illustrates how to invert a map. It is only possible to invert a map if no value is mapped to by two different keys. Comparing the number of elements in the Map.keySet method and the Map.valueSet method checks this. Otherwise, the elements are traversed in the incoming map and inserted in the result map. The function that does the inversion, that is, the invertMap method, will work regardless of the types of the keys and values.
{
Map example;
Map invertMap(map _mapToInvert)
{
MapEnumerator en;
Map result = new Map(
_mapToInvert.valueType(),
_mapToInvert.keyType());
if (_mapToInvert.keySet().elements()
!= _mapToInvert.valueSet().elements())
{
return null;
}
en = new MapEnumerator(_mapToInvert);
while (en.moveNext())
{
result.insert(en.currentValue(), en.currentKey());
}
return result;
}
;
// Fill in a few values.
example = new Map(Types::Integer, Types::String);
example.insert (1, "one");
example.insert (2, "two");
print invertMap(example).toString();
pause;
// Now two keys (2 and 3) map to the same value
// so can't create inverse map
example.insert (3, "two");
if (!invertMap(example))
{
print "Could not create the map";
}
pause;
}
Inheritance Hierarchy
Object Class
Map Class
InventTransMarkingMap Class
TaxCertificateInfo_NL Class
TaxErrorMessage_NL Class
WHSRFPassthrough Class