How to add a list of records to a map in Salesforce?
Normally we used to do something like this:
Map<Id,Contact> mapContact = new Map<Id,Contact>(); public static void addListToMap(List<Contact> conList){ for(Contact con:conList){ mapContact.put(con.id,con); } }
Here the processing time will be more(CPU execution time will be more), because we are iterating over a for loop to construct a map.
The best way to construct the map where we can avoid iteration like this:
public static void addListToMap(List<Contact> conList){ Map<Id,Contact> mapContact = new Map<Id,Contact>(conList); }
Here the processing time will be very less.