Maps – Collections – Apex Programming – Salesforce Certified Platform Developer I Study Guide

Maps: A map is a collection of key-value pairs, where each key is unique and maps to a corresponding value. Maps are often used to associate related data. Maps are useful when you want to associate related pieces of information and quickly retrieve the value associated with a specific key. The key and value in a Map can be of any data type: primitive types, sObjects, user-defined objects, and another. Apex provides various methods to manipulate maps. Some commonly used methods include

•\ put(): Adds or updates an element in the map

•\ get(): Returns the value associated with a specific key

•\ containsKey(): Checks if a specific key is present in the map

•\ remove(): Removes an element from the map

•\ keySet(): Returns a set of all keys in the map

•\ values(): Returns a list of all values in the map

For example:

Map<String, Integer> ageMap = new Map<String, Integer>();

ageMap.put(‘Alice’, 25);

ageMap.put(‘Bob’, 30);

System.debug(ageMap.get(‘Alice’));

Initialize an empty map named ageMap to store string keys (names) and their corresponding integer values (ages). Add two key-value pairs to the ageMap like Alice and Bob. Retrieve the value associated with the key “Alice” from the ageMap using ageMap.get(“Alice”), and print the retrieved value (25) to the system log using System. debug(ageMap.get(‘Alice’)).

As another example, you might use a Map to associate account names with their corresponding balances. In this example, the Map allows you to quickly retrieve the balance associated with a specific account name.

Map<String, Decimal> accountBalances = new Map<String, Decimal>(); accountBalances.put(‘Acct1’, 1000.00); accountBalances.put(‘Acct2’, 2500.00);

Decimal balance = accountBalances.get(‘Acct1’); System.debug(‘Balance of Acct1: ‘ + balance);

Here’s an example that demonstrates the commonly used methods containsKey(), remove(), keySet(), and values() for manipulating maps in Apex:

Map<String, Integer> studentMarks = new Map<String, Integer>();

  • Add key-value pairs to the map studentMarks.put(‘John’, 80); studentMarks.put(‘Emily’, 95); studentMarks.put(‘Michael’, 75);
  • Check if the map contains a specific key

Boolean containsJohn = studentMarks.containsKey(‘John’); System.debug(‘Does the map contain John? ‘ + containsJohn); // Output: Does the map contain John? true

  • Remove a key-value pair from the map studentMarks.remove(‘Michael’); System.debug(‘Updated map: ‘ + studentMarks);
  • Output: Updated map: {John=80, Emily=95}
  • Get a set of all keys in the map Set<String> keys = studentMarks.keySet(); System.debug(‘Keys in the map: ‘ + keys);
  • Output: Keys in the map: {John, Emily}
  • Get a list of all values in the map List<Integer> values = studentMarks.values(); System.debug(‘Values in the map: ‘ + values);
  • Output: Values in the map: (80, 95)

In this example, we have a map called studentMarks that stores the marks of different students. We use the put() method to add key-value pairs to the map, where the key represents the student’s name and the value represents the student’s marks.

We then demonstrate the usage of the containsKey() method to check if the map contains a specific key (“John” in this case).

Next, we use the remove() method to remove a key-value pair from the map, in this case, the entry for “Michael.”

We also demonstrate the keySet() method, which returns a set of all keys in the map, and the values() method, which returns a list of all values in the map.

Finally, we print the keys and values to verify the contents of the map.