This course is currently a work in progress. Please join the waitlist to receive regular updates.
How to initialize a map with values
Maps are empty when declared. To initialize a map with values use the following syntax.
Loading...
In the above example:
- In the above example, we have defined a map to hold the products and their respective quantities.
- The map key is a
string(product)
and value is aninteger(Quantity)
. - On the left of the
'=>'
symbol is the key and on the right is the value.
Initializing map values from SOQL
Map key-value pairs can be directly initialized from a SOQL query. To initialize a map from SOQL, use the following syntax:
Loading...
In the above example:
- In the above example, we have defined a map to hold the account id and account record.
- The Map
key
is an Id andvalue
is an account. - Apex can automatically map the Id to the
key
and the corresponding record to thevalue
from an SOQL query list.
Try It Yourself
Loading...