How to use collection classes - Map and Set



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.





static void TestMap(Args _args)
{

#define.Zip('Zipcode')

Map                     ZipCodeIdMap = new Map(Types::String, Types::Class);

MapEnumerator    mapEnumerator;

List                      listOfZipCodes = new List(Types::String);

ListEnumerator ZipcodesEnumerator;
str Zipid;



if (ZipCodeIdMap.exists(#Zip))
{

listOfZipCodes = ZipCodeIdMap.lookup(#Zip);

listOfZipCodes.addEnd('00501');
}
else
{
  listOfZipCodes = new List(Types::String);
 listOfZipCodes.addEnd('00501');
listOfZipCodes.addEnd('00501');
listOfZipCodes.addEnd('00502');
listOfZipCodes.addEnd('00503');
ZipCodeIdMap.insert(#Zip, listOfZipCodes);

}
mapEnumerator = ZipCodeIdMap.getEnumerator();

while (mapEnumerator.moveNext())
{

listOfZipCodes = mapEnumerator.currentValue();

ZipcodesEnumerator = listOfZipCodes.getEnumerator();

while (ZipcodesEnumerator.moveNext())
{

Zipid = ZipcodesEnumerator.current();

info(Zipid);

}

}

}
___


Set Class:


The Set class is used for the storage and retrieval of data from a collection in which the values of the elements contained are unique and serve as the key values according to which the data is automatically ordered.



static void TestSet(Args _args)

{
 Set ZipCode = new Set(Types::String);

SetEnumerator ZipCodeEnumerator;


LogisticsAddressZipCodeId logisticsAddressZipCodeId;


ZipCode.add('5001');

ZipCode.add('5001');

ZipCode.add('5001');

ZipCode.add('5002');

ZipCode.add('5003');

ZipCodeEnumerator = ZipCode.getEnumerator();

while(ZipCodeEnumerator.moveNext())

{

logisticsAddressZipCodeId = ZipCodeEnumerator.current();


info(logisticsAddressZipCodeId);


}


}

Comments