A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types. For example, the following table represents a map of countries and currencies:
| Country (Key) | 'United States' | 'Japan' | 'France' | 'England' | 'India' |
| Currency (Value) | 'Dollar' | 'Yen' | 'Euro' | 'Pound' | 'Rupee' |
Map keys and values can contain any collection, and can contain nested collections. For example, you can have a map of Integers to maps, which, in turn, map Strings to lists. Map keys can contain up to only four levels of nested collections.
To declare a map, use the Map keyword followed by the data types of the key and the value within <> characters. For example:
Map<String, String> country_currencies = new Map<String, String>(); Map<ID, Set<String>> m = new Map<ID, Set<String>>(); Map<ID, Map<ID, Account[]>> m2 = new Map<ID, Map<ID, Account[]>>();
You can use the generic sObject data type with maps. You can also create a generic instance of a map.
As with lists, you can populate map key-value pairs when the map is declared by using curly brace ({}) syntax. Within the curly braces, specify the key first, then specify the value for that key using =>. For example:
Map<String, String> MyStrings = new Map<String, String>{'a' => 'b', 'c' => 'd'.toUpperCase()}; Account[] accs = new Account[5]; // Account[] is synonymous with List<Account> Map<Integer, List<Account>> m4 = new Map<Integer, List<Account>>{1 => accs};
In the first example, the value for the key a is b, and the value for the key c is d. In the second, the key 1 has the value of the list accs.
Map<Account, String> mapKeyExample = new Map<Account, String>{ new Account(Name='Account1') => 'primary.person@account1.com', new Account(Name='Account2') => 'decision.maker@account2.com' };
To access elements in a map, use the Map methods provided by Apex. For example:
Account myAcct = new Account(); //Define a new account Map<Integer, Account> m = new Map<Integer, Account>(); // Define a new map m.put(1, myAcct); // Insert a new key-value pair in the map System.assert(!m.containsKey(3)); // Assert that the map contains a key Account a = m.get(1); // Retrieve a value, given a particular key Set<Integer> s = m.keySet(); // Return a set that contains all of the keys in the map
For more information, including a complete list of all supported Map methods, see Map Methods.
// Create an account and add it to the map Account a1 = new Account(Name='A1'); Map<sObject, Integer> m = new Map<sObject, Integer>{ a1 => 1}; // Get a1's value from the map.
// Returns the value of 1. System.assertEquals(1, m.get(a1)); // Id field is null. System.assertEquals(null, a1.Id); // Insert a1.
// This causes the ID field on a1 to be auto-filled
insert a1; // Id field is now populated. System.assertNotEquals(null, a1.Id); // Get a1's value from the map again.
// Returns null because Map.get(sObject) doesn't find
// the entry based on the sObject with an auto-filled ID.
// This is because when a1 was originally added to the map
// before the insert operation, the ID of a1 was null. System.assertEquals(null, m.get(a1));
Another scenario where sObject fields are autofilled is in triggers, for example, when using before and after insert triggers for an sObject. If those triggers share a static map defined in a class, and the sObjects in Trigger.New are added to this map in the before trigger, the sObjects in Trigger.New in the after trigger aren’t found in the map because the two sets of sObjects differ by the fields that are autofilled. The sObjects in Trigger.New in the after trigger have system fields populated after insertion, namely: ID, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, and SystemModStamp.