Map in apex salesforce

Map is a collection that is most useful while building complex logic in apex class or trigger, Specially while building trigger logic for bulkification. A map is basically a collection of key & value pairs where in each unique key maps to a single value. Keys and values can be any of below data type—
Primitive types like integer or string, collections, sObjects, user-defined types, and built-in Apex types.

To declare a map use keyword map as done in below example
 map<string,integer>countryCodeMap=New map<string,integer>()
above map is declared to store keys of type string and values of type integer.

Similar to lists and set you can directly populate map with key and values at the time of declaration as below,
Map<String, String> countryCodeMap = new Map<String, String>{'india' => 91, 'USA' => 1}; 
In the example above india and USA are the two keys and values 91 and 1 are their respective values.

In order to add key values we can also use map method provided by salesforce. In case of map we can use put method as below,
This adds new key china with a value 76.

Basically map has a keyset which is nothing but a set and values are nothing but a list. Key will always be unique and cannot be duplicated while values can be duplicate.

Let us see some important points on map

1. We can retrieve all the keys from map using keyset() method and it returns set hence the output of keyset() method should be stored in a set as below
Set<string> allcountiersSet = countryCodeMap.keyset()2. We can retrieve all the values from map using values() method and it return list hence the output of values() method should be stored in a list as below 
Set<integer>allCountryCodesList= countryCodeMap.values()

3. A map key can hold a null value and hence we need to take necessary care of putting null checks.

4. As discussed above key is always unique and cannot be duplicate hence if you add a key with a value wherein the key is already present in map then the existing values against the existing key is replaced.for example
This would mean key mexico has value 8 against it as 7 is overwritten by 8.

5. Multiple keys can have same value against it, as values can be duplicate.for example

6. You need to be very careful while having keys of type string, reason - string keys are case sensitive, Ignoring which can be disastrous.

7. Soql query can be stored directly in map of type (id,sobject) which gives sObject record against id of that record. example below
map<id,account> idaccountmap = map<id,account>(select id,name,accountsource from account limit 50) 
This gives account record against its id. This can be very useful  while building logic and also saves no of lines.

8. You can use get() method to get a value against a specific key in the map. for example

Using get method below
would return 91 and countryCodeMap.get('india') would return 7

1 comment:

  1. Can Somebody Explain Salesforce?
    <a href=">salesforce consulting services in Dallas</a>