Intermediate
25 min read
Maps
By the end of this lesson, you'll be able to:
- Declare and initialize a Map with a key type and a value type
- Add, retrieve, and check for the existence of a key
- Explain why a Map lookup by key is faster than searching a List
Prerequisites: "Sets"
Declaring and populating a Map
Map<String, Decimal> productPrices = new Map<String, Decimal>();
productPrices.put('Widget', 49.99);
productPrices.put('Gadget', 89.99);
System.debug(productPrices.get('Widget')); // 49.99
Map<String, Decimal> productPrices = new Map<String, Decimal>();—Map<String, Decimal>declares a map fromStringkeys toDecimalvalues.productPrices.put('Widget', 49.99);—put(key, value)adds an entry.productPrices.put('Gadget', 89.99);— adds another.- (blank line)
System.debug(productPrices.get('Widget'));—get(key)retrieves the value for that key directly — no looping required — logs49.99.
Checking for a key before using it
Map<String, Decimal> productPrices = new Map<String, Decimal>{'Widget' => 49.99};
if (productPrices.containsKey('Gadget')) {
System.debug(productPrices.get('Gadget'));
} else {
System.debug('No price found for Gadget.');
}
Map<String, Decimal> productPrices = new Map<String, Decimal>{'Widget' => 49.99};— a map with only one entry.- (blank line)
if (productPrices.containsKey('Gadget')) {—containsKey()checks safely first, exactly the guard-before-use habit from Module 12's "Handle Errors Gracefully" lesson.System.debug(productPrices.get('Gadget'));— only reached if the key exists.} else {— the key doesn't exist here.System.debug('No price found for Gadget.');— a clean fallback, instead of callingget()on a key that doesn't exist and silently gettingnullback — which could lead to a confusingNullPointerExceptionlater.}— closes theif/else.
Why a Map beats searching a List
// The List approach: search every item, one at a time
for (Product__c p : allProducts) {
if (p.Name == 'Widget') {
System.debug(p.Price__c);
break;
}
}
// The Map approach: direct lookup, no searching
System.debug(productPrices.get('Widget'));
// The List approach: search every item, one at a time— the label for what follows.for (Product__c p : allProducts) {— searching aListmeans checking items one by one.if (p.Name == 'Widget') {— until a match is found.System.debug(p.Price__c);— logs it.break;— stops searching once found.}— closes theif.}— closes the loop — slower as the list grows.- (blank line)
// The Map approach: direct lookup, no searching— the label for the alternative.System.debug(productPrices.get('Widget'));— aMaplooks a key up directly — why "look this up by Id" is almost always modeled as aMapin real Apex code, especially inside loops where repeated searching would be costly.
Exercise
Build a Map<String, Integer> called stockLevels with three products and their stock counts. Debug the stock level for one of them using get().
Show hint
stockLevels.put('Widget', 40);
Maps Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
A Map stores key-value pairs, letting you look up a value directly by its key instead of searching through a list one item at a time.