Collection | Map

 

Map Collection Class:

Map collection allows us to manage a collection of elements, where each element contains KEY and VALUE.

ü Map is a collection of Key – Value pairs.

ü Keys are always unique having a value associated.

ü Values can be duplicate.

ü Key and Value can be either Primitive / SObject / Apex / Collection / User Defined type.

ü Each Element can be identified by using KEY.

ü It will arrange the elements inside the collection based on the KEY in Sorting order.

ü Map is an Unordered collection.

ü Map Collection maintains the uniqueness, based on the KEY. Hence map collection doesn’t allow duplicate elements.


Syntax:

Map<KeyDatatype, ValueDatatype> <objName> = new Map<KeyDatatype, ValueDatatype>();

            Example:

            Map<Integer, Integer> customerCodes = new Map<Integer, Integer>();

            Map<Id, String> recordDetails = new Map<Id, String>();

            Map<String, String> countryCodes = new Map<String, String>();

            Map<Id, Account> accountMap = new Map<Id, Account>();

            Map<Id, Position__c> positionMap = new Map<Id, Position__C>();

            Map<Account, List<contact>> contactsMap = new Map<Account, List<contat>>();


Post a Comment

0 Comments