Experience Salesforce

Map Initialization Methods in APEX

What You’ll Learn

  • Initialisation Of Map
  • Map Of List In Apex
  • Methods Of Map

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

  1. Initialize an Empty Map of String to Integer:
Map<String, Integer> myMap = new Map<String, Integer>();
  1. Initialize an Empty Map of ID to Custom Object:
Map<Id, MyCustomObject__c> myMap = new Map<Id, MyCustomObject__c>();

Map Initialization with Values

  1. Initialize a Map with String Keys and Integer Values:
Map<String, Integer> myMap = new Map<String, Integer>{
    ‘One’ => 1,
    ‘Two’ => 2,
    ‘Three’ => 3
};
  1. 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.NoFunctionExample
1clear()
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());
2clone()
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);
3containsKey(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);
4deepClone()
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));
5equals(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);
6get(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);
7getSObjectType()
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);
8hashCode()
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);
9isEmpty()
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);
10keySet()
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);
11put(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’));
12putAll(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());
13putAll(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());
14remove(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’));
15size()
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);
16toString()
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);
17values()
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);
Next Topic

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.

cts-img
Rakshabandhan Sale