Map Initialization Methods in APEX
Chapter Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
Initialisation of Maps in Apex
In Salesforce Apex, Maps are collections of key-value pairs, where each unique key maps to a single value. Initializing of Maps in Apex can be done in various ways depending on the requirements. Below are the different methods to initialize maps in Apex.
Empty Map Initialization
- Initialize an Empty Map of String to Integer:

- Initialize an Empty Map of ID to Custom Object:

Map Initialization with Values
- Initialize a Map with String Keys and Integer Values:

- Initialize a Map with String Keys and Custom Object Values:

Map Initialization with SOQL Query
Initialize a Map from a SOQL Query:

Map Methods in Apex
Following are the Map methods in Apex:
S.No | Function | Example |
---|---|---|
1 | clear() Removes all of the key-value mappings from the map. |
Map<String, Integer> myMap = new Map<String, Integer>{'a' => 1, 'b' => 2}; |
2 | clone() Makes a duplicate copy of the map. |
Map<String, Integer> originalMap = new Map<String, Integer>{'a' => 1, 'b' => 2}; |
3 | containsKey(key) Returns true if the map contains a mapping for the specified key. |
Map<String, Integer> myMap = new Map<String, Integer>{'a' => 1, 'b' => 2}; |
4 | deepClone() Makes a duplicate copy of a map, including sObject records if this is a map with sObject record values. |
Map<Id, Account> originalMap = new Map<Id, Account>{}; |
5 | equals(map2) Compares this map with the specified map and returns true if both maps are equal; otherwise, returns false. |
Map<String, Integer> map1 = new Map<String, Integer>{'a' => 1, 'b' => 2}; |
6 | get(key) Returns the value to which the specified key is mapped, or null if the map contains no value for this key. |
Map<String, Integer> myMap = new Map<String, Integer>{'a' => 1, 'b' => 2}; |
7 | getSObjectType() Returns the token of the sObject type that makes up the map values. |
Map<Id, Account> accountMap = new Map<Id, Account>{}; |
8 | hashCode() Returns the hashcode corresponding to this map. |
Map<String, Integer> myMap = new Map<String, Integer>{'a' => 1, 'b' => 2}; |
9 | isEmpty() Returns true if the map has zero key-value pairs. |
Map<String, Integer> myMap = new Map<String, Integer>(); |
10 | keySet() Returns a set that contains all of the keys in the map. |
Map<String, Integer> myMap = new Map<String, Integer>{'a' => 1, 'b' => 2}; |
11 | put(key, value) Associates the specified value with the specified key in the map. |
Map<String, Integer> myMap = new Map<String, Integer>(); |
12 | putAll(fromMap) Copies all of the mappings from the specified map to the original map. |
Map<String, Integer> map1 = new Map<String, Integer>{'a' => 1}; |
13 | putAll(sobjectArray) Adds the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>. |
List<;Account> accountList = [SELECT Id, Name FROM Account LIMIT 10]; |
14 | remove(key) Removes the mapping for the specified key from the map, if present, and returns the corresponding value. |
Map<String, Integer> myMap = new Map<String, Integer>{'a' => 1, 'b' => 2}; |
15 | size() Returns the number of key-value pairs in the map. |
Map<String, Integer> myMap = new Map<String, Integer>{'a' => 1, 'b' => 2}; |
16 | toString() Returns the string representation of the map. |
Map<String, Integer> myMap = new Map<String, Integer>{'a' => 1, 'b' => 2}; |
17 | values() Returns a list that contains all the values in the map. |
Map<String, Integer> myMap = new Map<String, Integer>{'a' => 1, 'b' => 2}; |
Join our newsletter: Get daily update on Salesforce career insights & news!
Join Now!
Need more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.