Experience Salesforce
Map Initialization Methods in APEX
What You’ll Learn
- Initialisation Of Map
- Map Of List In Apex
- Methods Of Map
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:
Map<String, Integer> myMap = new Map<String, Integer>(); |
- Initialize an Empty Map of ID to Custom Object:
Map<Id, MyCustomObject__c> myMap = new Map<Id, MyCustomObject__c>(); |
Map Initialization with Values
- Initialize a Map with String Keys and Integer Values:
Map<String, Integer> myMap = new Map<String, Integer>{ ‘One’ => 1, ‘Two’ => 2, ‘Three’ => 3 }; |
- Initialize a Map with String Keys and Custom Object Values:
MyCustomObject__c obj1 = new MyCustomObject__c(Name = ‘Object1’); MyCustomObject__c obj2 = new MyCustomObject__c(Name = ‘Object2’); Map<String, MyCustomObject__c> myMap = new Map<String, MyCustomObject__c>{ ‘Key1’ => obj1, ‘Key2’ => obj2 }; |
Map Initialization with SOQL Query
Initialize a Map from a SOQL Query:
Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Id, Name FROM Account WHERE Industry = ‘Technology’]); |
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}; myMap.clear(); System.assertEquals(0, myMap.size()); |
2 | clone() Makes a duplicate copy of the map. | Map<String, Integer> originalMap = new Map<String, Integer>{‘a’ => 1, ‘b’ => 2}; Map<String, Integer> clonedMap = originalMap.clone(); System.assertEquals(originalMap, clonedMap); |
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}; Boolean hasKey = myMap.containsKey(‘a’); System.assertEquals(true, hasKey); |
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>{}; Account acc = new Account(Name=’Test Account’); insert ACC; originalMap.put(acc.Id, ACC); Map<Id, Account> clonedMap = originalMap.deepClone(); System.assertEquals(originalMap.get(acc.Id), clonedMap.get(acc.Id)); |
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}; Map<String, Integer> map2 = new Map<String, Integer>{‘a’ => 1, ‘b’ => 2}; Boolean isEqual = map1.equals(map2); System.assertEquals(true, isEqual); |
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}; Integer value = myMap.get(‘a’); System.assertEquals(1, value); |
7 | getSObjectType() Returns the token of the sObject type that makes up the map values. | Map<Id, Account> accountMap = new Map<Id, Account>{}; Schema.SObjectType sObjectType = accountMap.getSObjectType(); System.assertEquals(Account.SObjectType, sObjectType); |
8 | hashCode() Returns the hashcode corresponding to this map. | Map<String, Integer> myMap = new Map<String, Integer>{‘a’ => 1, ‘b’ => 2}; Integer hash = myMap.hashCode(); System.debug(‘Hash code: ‘ + hash); |
9 | isEmpty() Returns true if the map has zero key-value pairs. | Map<String, Integer> myMap = new Map<String, Integer>(); Boolean empty = myMap.isEmpty(); System.assertEquals(true, empty); |
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}; Set<String> keys = myMap.keySet(); System.assertEquals(new Set<String>{‘a’, ‘b’}, keys); |
11 | put(key, value) Associates the specified value with the specified key in the map. | Map<String, Integer> myMap = new Map<String, Integer>(); myMap.put(‘a’, 1); System.assertEquals(1, myMap.get(‘a’)); |
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}; Map<String, Integer> map2 = new Map<String, Integer>{‘b’ => 2}; map1.putAll(map2); System.assertEquals(2, map1.size()); |
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]; Map<Id, Account> accountMap = new Map<Id, Account>(); accountMap.putAll(accountList); System.assertEquals(accountList.size(), accountMap.size()); |
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}; Integer removedValue = myMap.remove(‘a’); System.assertEquals(1, removedValue); System.assertEquals(false, myMap.containsKey(‘a’)); |
15 | size() Returns the number of key-value pairs in the map. | Map<String, Integer> myMap = new Map<String, Integer>{‘a’ => 1, ‘b’ => 2}; Integer size = myMap.size(); System.assertEquals(2, size); |
16 | toString() Returns the string representation of the map. | Map<String, Integer> myMap = new Map<String, Integer>{‘a’ => 1, ‘b’ => 2}; String mapString = myMap.toString(); System.debug(‘Map as String: ‘ + mapString); |
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}; List<Integer> values = myMap.values(); System.assertEquals(new List<Integer>{1, 2}, values); |
Need Extra Support? Our FREE study materials have got you covered.
Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.