Is it possible to return a custom map from a SOQL query?
I know we can do this:
Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT Id ... FROM Contact]);
bus is it also possible to have something like this:
Map<String, Decimal> myMap = new Map<String, Decimal>(SELECT name, Amount__c FROM Cust_Obj);
I want the above map to be populated like this, without iterating through a list:
for(Cust_Obj CO : Cust_Obj_List) {
myMap.put(CO.name, CO.Amount__c);
}
Best Answer
No. You need to loop through for any mapping strategy besides by
Id
. Thefor
loop is required to map byName
.There are only three defined constructors on the
Map Class
:There's also the
putAll
method but it gives you the same result: