+ Start a Discussion
DeveloperDeveloper 

when do we use list? when do we use map? when do we use set? exmples

Best Answer chosen by Developer
Amit Chaudhary 8Amit Chaudhary 8
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>();

User-added image
Please look into below example which will give you an idea about List, Set and Map.
 
Example:
public class CollectionExample {
    public void printList() {
        List<Integer> li = new List<Integer>();
       
        li.add(1);
        li.add(5);
        li.add(3);
        li.add(1);
        li.add(2);
        li.add(4);
        li.add(6);
        li.add(4);
        li.add(8);
        li.add(7);
       
        System.debug('========================List li:'+li);
        // ========================List li:(1, 5, 3, 1, 2, 4, 6, 4, 8, 7)
       
        for(Integer val: li) {
            System.debug('---------------------Value: '+val);
        }
    }
   
    public void printSet() {
        Set<Integer> se = new Set<Integer>();
       
        se.add(1);
        se.add(5);
        se.add(3);
        se.add(1);
        se.add(2);
        se.add(4);
        se.add(6);
        se.add(4);
        se.add(8);
        se.add(7);
       
        System.debug('========================Set se:'+se);
        // ========================List li:(1, 5, 3, 1, 2, 4, 6, 4, 8, 7)
        //========================Set se:{1, 2, 3, 4, 5, 6, 7, 8}
       
        for(Integer val: se) {
            System.debug('---------------------Value: '+val);
        }
    }
   
    public void printMap() {
        Map<Integer, String> ma = new Map<Integer, String>();
       
        ma.put(1, 'Anu');
        ma.put(5, 'Ash');
        ma.put(3, 'Ven');
        ma.put(1, 'Swa');
        ma.put(2, 'Sar');
        ma.put(4, 'Div');
        ma.put(6, 'Dee');
        ma.put(4, 'Sac');
        ma.put(8, 'Swa');
        ma.put(7, 'She');
       
        System.debug('========================Map ma:'+ma);
        // ========================List li:(1, 5, 3, 1, 2, 4, 6, 4, 8, 7)
        // ========================Set se:{1, 2, 3, 4, 5, 6, 7, 8}
        // ========================Map ma:{1=Swa, 2=Sar, 3=Ven, 4=Sac, 5=Ash, 6=Dee, 7=She, 8=Swa}
       
        Set<Integer> kSet = ma.keySet();
        // ==============kSet:{1, 2, 3, 4, 5, 6, 7, 8}
       
        System.debug('==============kSet:'+kSet);
       
        for(Integer key: kSet) {
            System.debug('---------------------Key: '+key);
            System.debug('---------------------Value: '+ma.get(key));
        }
       
        List<String> valList = ma.values();
       
        for(String val: valList) {
            System.debug('---------------------Value: '+val);
        }
       
    }
}
***************
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
trigger ContactTriggerWithList on Contact (before insert) {
    // Here taking the Set because we don't need to maintain duplicate
    Set<Id> accIdSet = new Set<Id>();
    
    for(Contact con: Trigger.new) {
        if(con.AccountId != null) {
            accIdSet.add(con.AccountId);
        }
    }
    
    if(!accIdSet.isEmpty()) {
        List<Account> accList = [Select Id, Name, Type from Account where Id IN: accIdSet];
        
        for(Contact con: Trigger.new) {
            if(con.AccountId != null) {
                for(Account acc: accList) {
                    if(con.AccountId == acc.Id) {
                        con.Acc_Type__c = acc.Type;
                    }
                }
            }
        }
    }
}
Same Trigger using the Map
trigger ContactTriggerWithMap on Contact (before insert) {
    // Here taking the Set because we don't need to maintain duplicate
    Set<Id> accIdSet = new Set<Id>();
    
    for(Contact con: Trigger.new) {
        if(con.AccountId != null) {
            accIdSet.add(con.AccountId);
        }
    }
    
    if(!accIdSet.isEmpty()) {
        Map<Id, Account> accMap = new Map<Id, Account>([Select Id, Name, Type from Account where Id IN: accIdSet]);
        
        for(Contact con: Trigger.new) {
            if(con.AccountId != null) {
                con.Acc_Type__c = accMap.get(con.AccountId).Type;
            }
        }
    }
}

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.