Salesforce apex map methods with practice examples
A map is a data structure in Salesforce that links unique keys to specific values in Apex. Mastering Apex maps can be a bit tricky, so we've covered the most important map methods with practice examples in this article.
Key topics
How to write a map in Apex
Maps in Salesforce Apex are collections of key-value pairs. The keys are unique, and each key maps to a specific value. Here's an example of fruit and quantities:
Map<String, Integer> fruitsAndQuantities = new Map<String, Integer>{
'Apples' => 50,
'Oranges' => 30,
'Bananas' => 20
};
What are the apex map methods
Salesforce Apex offers a robust set of map methods. These predefined methods help developers solve complex business requirements and address governor limit exceptions.
The following are some of the most important map methods in apex
1. put
This method adds a key-value pair to the map. If the key already exists in the map, the value will be overwritten. Keys are case sensitive in apex maps.
Map mainMap = Map<String, Integer> product = new Map<String, Integer>();
product.put('Apples', 50);
System.debug('Product : ' + product); // Product : {Apples=50}
2. get
This method returns the value associated with a specified key. If the key is not found in the map, null is returned.
Map<String, Integer> productQuantities = new Map<String, Integer>{
'Apples' => 50,
'Oranges' => 30,
'Bananas' => 20
};
Integer orangesQuantity = productQuantities.get('Oranges');
System.debug('Quantity of Oranges: ' + orangesQuantity); // Quantity of Oranges: 30
3. containsKey
To find if the key exist in a map use the containsKey method. This method returns true if the map contains the specified key, otherwise it returns false.
Map mainMap = Map<String, Integer> productQuantities = new Map<String, Integer>{
'Apples' => 50,
'Oranges' => 30,
'Bananas' => 20
};
Boolean hasOranges = productQuantities.containsKey('Oranges');
System.debug('Contains Oranges: ' + hasOranges); // Contains Oranges: true
Boolean hasGrapes = productQuantities.containsKey('Grapes');
System.debug('Contains Grapes: ' + hasGrapes); // Contains Grapes: false
4. remove
This method removes an entry from the map for a specific key. If the key is not found in the map, no action is taken.
Map<String, Integer> productQuantities = new Map<String, Integer>{
'Apples' => 50,
'Oranges' => 30,
'Bananas' => 20
};
Integer removedValue = productQuantities.remove('Oranges');
System.debug('Removed value: ' + removedValue); // Removed value: 30
System.debug('Final Map: ' + productQuantities); // Final Map: {Apples=50, Bananas=20}
5. keySet
To find all keys of the map use the keySet method. This method returns a set of keys of the given map.
Map<String, Integer> productQuantities = new Map<String, Integer>{
'Apples' => 50,
'Oranges' => 30,
'Bananas' => 20
};
Set<String> keys = productQuantities.keySet();
System.debug('Keys in the Map: ' + keys); // Keys in the Map: {Apples, Oranges, Bananas}
6. values
To find all values from a map use the values() method.
Map<String, Integer> productQuantities = new Map<String, Integer>{
'Apples' => 50,
'Oranges' => 30,
'Bananas' => 20
};
List<Integer> quantities = productQuantities.values();
System.debug('Product Quantities: ' + quantities);//Product Quantities: (50, 30, 20)
7. size
To find the number of elements in the map, use the size method.
Map<String, Integer> productQuantities = new Map<String, Integer>{
'Apples' => 50,
'Oranges' => 30,
'Bananas' => 20
};
Integer mapSize = productQuantities.size();
System.debug('Size of the Map: ' + mapSize); //Size of the Map: 3
8. clear
To remove all key-value pairs from the map, use the clear method.
Map<String, Integer> productQuantities = new Map<String, Integer>{'Apples' => 50};
productQuantities.clear();
System.debug('Product Quantities after clear: ' + productQuantities);
// Product Quantities after clear: {}
9. isEmpty
To find if the map has any key-value pairs, use the empty method. The empty method returns true if the map does not contain any key-value pairs otherwise return false.
Map inputMap = Map<String, Integer> emptyMap = new Map<String, Integer>();
Boolean isEmptyMap = emptyMap.isEmpty();
System.debug('Is the Map empty? ' + isEmptyMap); // Is the Map empty? true