1 |
Map<id, Contact> |
1 |
Map<string, Contact> |
1 |
for |
1 |
Map<id , Contact> |
1 2 3 4 5 |
Map<Id, Contact> cs = new Map<Id, Contact>(); for ( Contact c : [SELECT id FROM Contact WHERE accountid IN :trigger.newMap.keyset()]) { cs.put(c.id, c); } |
1 2 3 |
Map<Id, Contact> cs = new Map<Id, Contact>( [select id FROM Contact WHERE accountid IN :trigger.newMap.keyset()] ); |
Good catch. One thing to note that takes this one step further: the constructor for Map can always take a List, which is essentially what you are using in your new way.
So this can be used anywhere. It can be especially useful when using dynamic soql and the Database.query() method, which returns a List, but when you want a map, like this:
String query = ‘select Id, Name from Account’;
Map accountsMap = new Map((List)Database.query(query));
…(note that Database.query returns a List always, so it can be casted into the strongly-typed list of the type you want, which is what is happening here)
It can also be especially useful when you need to make a map out of a child collection of an SObject, such as the “Contacts” collection of an Account record. For example, when you query an account with all of its contacts …
Account a = [select Id, Name, (select Id, Name from Contacts) from Account limit 1];
… since the “Contacts” collection is just a List, you can make a Map like this:
Map contactsMap = new Map(a.Contacts);
Does this assumes that the map you want is formatted as map with key being the ID of the sobject?
IF I want a map where the id is a lookup value in the sobject, or a map with the string a text/number value in the sobject I would still need to create a loop. Am I correct with this assumption?
Exactly, Brian. This puts the sfdcID as the key, and the sObject as the value. (A map is basically a list of key-value pairs where the keys are unique within the keyset.)
Using any other field as the key requires a loop.
Thanks David – although I was hoping I was wrong with my assuming and found a more awesome way to creating my maps!
Hi.
Its great to see the map concept without loops.
My question is like : Is it possible to have map =[SOQL Query];
I know its a kind of mad question but if it exist , we could reduce the loops as you did in this blog .
Anyway thanks a lot in advance .
That is close to what I did, but we have to differentiate between a Map and a List. There is a limitation (feature?) in Apex, as in Java or C++, where one must instantiate a new map before populating it.
Apex does let you create a List without instantiating it, and that is what one would get from your query:
Bottom line: Maps must be instantiated with a “new” call. Lists are what one gets when not instantiating a Map. I hope this helps.