Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
Vasu.P

# Can any one Provide me the Best and Easy way to understand the Map Concept

Developer
Hi Vasu
please find out bellow infermation about List, Set, Map.

List,Set,Map are called collections in Apex:
List: A list is an ordered collection
so use list when you want to identify list element based on Index Number.(Lsit can contain Duplicates)
EX: List<Account> accList = new List<Account>();

Set: A set is an unordered collection of primitives or sObjects that do not contain any duplicate elements.
So, use set if you want to make sure that your collection should not contain Duplicates.
EX: Set<Account> accSet = new Set<Account>()

Map: A map is a collection of key-value pairs where each unique key maps to a single value. Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object.
EX: Map<Id, Account> accMap = new Map<Id, Account>();

Please look into below example which will give you an idea about List, Set and Map.

Example:
01public class CollectionExample {
02    public void printList() {
03        List<Integer> li = new List<Integer>();
04
15
16        System.debug('========================List li:'+li);
17        // ========================List li:(1, 5, 3, 1, 2, 4, 6, 4, 8, 7)
18
19        for(Integer val: li) {
20            System.debug('---------------------Value: '+val);
21        }
22    }
23
24    public void printSet() {
25        Set<Integer> se = new Set<Integer>();
26
37
38        System.debug('========================Set se:'+se);
39        // ========================List li:(1, 5, 3, 1, 2, 4, 6, 4, 8, 7)
40        //========================Set se:{1, 2, 3, 4, 5, 6, 7, 8}
41
42        for(Integer val: se) {
43            System.debug('---------------------Value: '+val);
44        }
45    }
46
47    public void printMap() {
48        Map<Integer, String> ma = new Map<Integer, String>();
49
50        ma.put(1, 'Anu');
51        ma.put(5, 'Ash');
52        ma.put(3, 'Ven');
53        ma.put(1, 'Swa');
54        ma.put(2, 'Sar');
55        ma.put(4, 'Div');
56        ma.put(6, 'Dee');
57        ma.put(4, 'Sac');
58        ma.put(8, 'Swa');
59        ma.put(7, 'She');
60
61        System.debug('========================Map ma:'+ma);
62        // ========================List li:(1, 5, 3, 1, 2, 4, 6, 4, 8, 7)
63        // ========================Set se:{1, 2, 3, 4, 5, 6, 7, 8}
64        // ========================Map ma:{1=Swa, 2=Sar, 3=Ven, 4=Sac, 5=Ash, 6=Dee, 7=She, 8=Swa}
65
66        Set<Integer> kSet = ma.keySet();
67        // ==============kSet:{1, 2, 3, 4, 5, 6, 7, 8}
68
69        System.debug('==============kSet:'+kSet);
70
71        for(Integer key: kSet) {
72            System.debug('---------------------Key: '+key);
73            System.debug('---------------------Value: '+ma.get(key));
74        }
75
76        List<String> valList = ma.values();
77
78        for(String val: valList) {
79            System.debug('---------------------Value: '+val);
80        }
81
82    }
83}
***************
There is a realtime situation where-in we need to loop through the collection of records and get the appropriate value from the matching record.

Requirement is:
Using trigger populate the Account Type on the Contact record (only insert scenario).

To achieve this requirement, here I am mentioning using trigger, so planning to write a trigger:

If we use List and not Map
01trigger ContactTriggerWithList on Contact (before insert) {
02    // Here taking the Set because we don't need to maintain duplicate
03    Set<Id> accIdSet = new Set<Id>();
04
05    for(Contact con: Trigger.new) {
06        if(con.AccountId != null) {
08        }
09    }
10
11    if(!accIdSet.isEmpty()) {
12        List<Account> accList = [Select Id, Name, Type from Account where Id IN: accIdSet];
13
14        for(Contact con: Trigger.new) {
15            if(con.AccountId != null) {
16                for(Account acc: accList) {
17                    if(con.AccountId == acc.Id) {
18                        con.Acc_Type__c = acc.Type;
19                    }
20                }
21            }
22        }
23    }
24}
Same Trigger using the Map
01trigger ContactTriggerWithMap on Contact (before insert) {
02    // Here taking the Set because we don't need to maintain duplicate
03    Set<Id> accIdSet = new Set<Id>();
04
05    for(Contact con: Trigger.new) {
06        if(con.AccountId != null) {
08        }
09    }
10
11    if(!accIdSet.isEmpty()) {
12        Map<Id, Account> accMap = new Map<Id, Account>([Select Id, Name, Type from Account where Id IN: accIdSet]);
13
14        for(Contact con: Trigger.new) {
15            if(con.AccountId != null) {
16                con.Acc_Type__c = accMap.get(con.AccountId).Type;
17            }
18        }
19    }
20}
If you compare both the triggers,

Trigger 1 is having a List of Accounts, where in we have to loop through the matching Account every time to populate the Acc_Type in the second for loop.

Trigger 2 is having a Map of Accounts with Id and Account as the Datatypes. Hence we can directly get the corresponding Account record and populate the Acc_Type easily.

Hope this will clear you actual doubt.

Please do let me know if it helps you.

Nagendra (Salesforce Developers)
Hi,

A map is used to store your data.. it is one of the collections Salesforce provides... it helps you hold data temporarily (in apex code) that helps build your logic.

A map has two components key and value.

map<key,value>

it helps you store data with relationships...

It will be good to understand LIst and Set (List and set are other collections used in Salesforce) and then visit this page to know more about maps.
All about collections (List, set and Map) here Hope this helps.

Please mark this as solved if it's resolved.

Thanks,
Nagendra