I tried all the suggestions with an example and found the following results.
Map<Id,List<Contact>> mapContactIdLstAccount = new Map<Id,List<Contact>>();
system.debug(system.now() + ' ' + system.now().millisecond());
for(Contact c : [select AccountId from Contact limit 200 offset 0]){
List<Contact> lstContact = new List<Contact>();
if(mapContactIdLstAccount.containsKey(c.AccountId))
lstContact = mapContactIdLstAccount.get(c.AccountId);
lstContact.add(c);
mapContactIdLstAccount.put(c.AccountId,lstContact);
}
system.debug(system.now() + ' ' + system.now().millisecond());
281 Milliseconds - From original Question
Map<Id,List<Contact>> mapContactIdLstAccount = new Map<Id,List<Contact>>();
system.debug(system.now() + ' ' + system.now().millisecond());
for(Contact c : [select AccountId from Contact limit 200 offset 0]){
if(mapContactIdLstAccount.get(c.AccountId) != NULL)
mapContactIdLstAccount.get(c.AccountId).add(c);
else
mapContactIdLstAccount.put(c.AccountId,new List<Contact>{c});
}
system.debug(system.now() + ' ' + system.now().millisecond());
154 Milliseconds - From original Question
Map<Id,List<Contact>> mapContactIdLstAccount = new Map<Id,List<Contact>>();
system.debug(system.now() + ' ' + system.now().millisecond());
for(Contact c : [select AccountId from Contact limit 200 offset 0]) {
if(!mapContactIdLstAccount.containsKey(c.AccountId))
mapContactIdLstAccount.put(c.AccountId, New Contact[]{});
mapContactIdLstAccount.get(c.AccountId).add(c);
}
system.debug(system.now() + ' ' + system.now().millisecond());
232 Milliseconds - From Eric's Answer
Map<Id,List<Contact>> mapContactIdLstAccount = new Map<Id,List<Contact>>();
system.debug(system.now() + ' ' + system.now().millisecond());
for(Contact c : [select AccountId from Contact limit 200 offset 0]) {
if(mapContactIdLstAccount.containsKey(c.AccountId)) {
mapContactIdLstAccount.get(c.AccountId).add(c);
} else {
mapContactIdLstAccount.put(c.AccountId, new Contact[]{c});
}
}
system.debug(system.now() + ' ' + system.now().millisecond());
156 Milliseconds - From SFDCFox Answer
Map<Id,List<Contact>> mapContactIdLstAccount = new Map<Id,List<Contact>>();
system.debug(system.now() + ' ' + system.now().millisecond());
for (Contact c : [select AccountId from Contact limit 200 offset 0]) {
List<Contact> lstContact = mapContactIdLstAccount.get(c.AccountId);
if (lstContact == null) {
lstContact = new List<Contact>();
mapContactIdLstAccount.put(c.AccountId, lstContact);
}
lstContact.add(c);
}
system.debug(system.now() + ' ' + system.now().millisecond());
148 Milliseconds - From Keith Answer
Looking at the results, selecting Keith's answer as Accepted Answer. Thank you all for your time.