0 out of 2 rated this helpful - Rate this topic

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.


class Map extends Object

Called

Method

Description

cancelTimeOut

Cancels a previous method call to the Object.setTimeOut Method. (Inherited from Object.)

definitionString

Returns a string that contains the definition of the map. For example: [str -> int].

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

Checks 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. This 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

Overridden. Creates a new map. The key type and value type are specified by the parameters.

notify

Releases the hold on an object that has called a wait method on this object. (Inherited from Object.)

notifyAll

Releases a lock on the object that was issued by a 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

Overridden. Returns a description of the (key, value) pairs in the map.

usageCount

Returns the current number of references (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

Overridden.

::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.

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.

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 (invertMap) 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;
}
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ