Map in D365 F&O - X++ Code

Rumman Ansari   Software Engineer   2023-08-29   2526 Share
☰ Table of Contents

Table of Content:


Map in D365 F&O - X++ Code

Important Points about Map

  • The Map class lets to associate one value (the key) with another value.
  • Both the key and the 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.
  • 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 that has an existing key value, it replaces the existing pair with that key value.
  • The (key, value) pairs in a map can be traversed by using the MapEnumerator class.
Map in D365 F&O - X++ Code

How to create a Map in X++ Programming Language


Map map = new Map(Types::String, Types::Integer);

Explanation:

  1. Map: The keyword "Map" represents the data structure being used. In X++, a Map is a collection that stores key-value pairs.
  2. map: This is the name of the variable being declared to store the newly created Map object.
  3. =: The equal sign is used for assignment, indicating that the variable map will be assigned the value on the right side of the equal sign.
  4. new Map(): This is the constructor used to create a new instance of the Map class. The constructor requires two parameters: the data type of the key and the data type of the value.
  5. Types::String: This specifies the data type of the key. In this case, the key is of type String.
  6. Types::Integer: This specifies the data type of the value. In this case, the value is of type Integer.

In summary, the line of code creates a new Map object named map that can store key-value pairs, where the keys are of type String and the values are of type Integer. This map can be used to associate strings with integer values, providing a flexible and efficient way to store and retrieve data relationships.


How to Insert inside a Map


// Adding values to the map
    map.insert("John", 25);

How to Access a Map


 // Accessing values in the map
    int johnsAge = map.lookup("John");

How to Iterating through the map


 // Iterating through the map
    MapEnumerator enumerator = map.getEnumerator();
    
    while (enumerator.moveNext())
    {
        str name = enumerator.currentKey();
        int age = enumerator.currentValue();
        
        info(strFmt("%1's age: %2", name, age));
    }

Check that a particular key is present or not


boolean check ;

Map map1 = new Map(Types::Int64, Types::String);

map1.add(1, "A");
map1.add(1, "C");
map1.add(2, "D");

check = map1.exists(2);

if (check) {
    anytype val = map1.lookup(2);
}

Whole Code


static void MapExample(Args _args)
{
    Map map = new Map(Types::String, Types::Integer);
    
    // Adding values to the map
    map.insert("John", 25);
    map.insert("Alice", 30);
    map.insert("Bob", 35);
    
    // Accessing values in the map
    int johnsAge = map.lookup("John");
    int alicesAge = map.lookup("Alice");
    
    info(strFmt("John's age: %1", johnsAge));
    info(strFmt("Alice's age: %1", alicesAge));
    
    // Updating a value in the map
    map.insert("Bob", 40);
    
    // Removing a key-value pair from the map
    map.remove("Alice");
    
    // Iterating through the map
    MapEnumerator enumerator = map.getEnumerator();
    
    while (enumerator.moveNext())
    {
        str name = enumerator.currentKey();
        int age = enumerator.currentValue();
        
        info(strFmt("%1's age: %2", name, age));
    }
}

Output:


John's age: 25
Bob's age: 40
Alice's age: 30
John's age: 25