Sometimes, we need to copy a list of sObject records into Map of, record id as a key and record as a value. here is a simple solution to how to do that instead of looping through list and add one by one record to map.
Copy List to Map using for Loop:
// query account records
List<Account> acLst = [select id, name from Account limit 100];
// create a map
Map<Id, Account> acMap = New Map<Id, Account>();
// loop throught and add one by one record to map
for(Account ac: acLst){
acMap.put(ac.id, ac);
}
Copy List to Map quick way:
// query account records
List<Account> acLst = [select id, name from Account limit 100];
// create a map and copy all records
Map<Id, Account> acMap = New Map<Id, Account>(acLst);