Convert List To Map in Apex Salesforce

Sometimes, we need to convert list of elements to map. instead of looping each element and put that into a map, here is a simple trick to do that in single step. 

List to Map conversion in apex

List<Account> accountsLst = [SELECT Id, Name FROM Account];
// this map holds, account id as a key and account object as value
Map<Id, Account> accountMap = new Map<Id, Account> (accountsLst);
Labels:
Join the conversation