Here is the simple way to convert list of sobject records to map.
Assigning query result to List:
Assigning query result to Map:
Converting List to Map:
Assigning query result to List:
- //Assigning return query result to the list.
- List<Lead> leadList = [SELECT Id, Name FROM Lead];
Assigning query result to Map:
- //Directly assigning return query result to the map.
- Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Id, Name FROM Lead]);
Converting List to Map:
- //Converting list to map
- Map<Id, Lead> mapFromList = new Map<Id, Lead>(leadList);