Maps are a critical data type to get to know when developing on the Force.com platform
- A map is a collection of key-value pairs where key should be unique and value can be duplicated.
- Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
- Map keys of type String are case-sensitive
- Map keys can contain up to only four levels of nested collections.
- A map key can hold the null value.
- Adding a map entry with a key that matches an existing key in the map overwrites the existing entry with that key with the new entry.
- Uniqueness of map keys of user-defined types is determined by the equals and hashCode,which we provide in your classes.
- Uniqueness of keys of all other non-primitive types, such as sObject keys, is determined by comparing the objects’ field values.
Syntax:
Map<Key,Value> variablename=new Map<Key,Value>();
Map<integer, string> mapName = new Map<integer, string>();
If you have a list of sObjects you can just pass that list in the constructor
Map<Id, Account> accountsById = new Map<Id, Account>(listOfAccounts);
keySet()
values()
get(Keyid)
put(key,value)
putAll(fromMap)
putAll(sobjectArray)
containskey(key)
clear()
size()
clone()
isEmpty()
remove(key)
deepClone()
equals(map2)
getSobjectType()
hashCode()
Let’s make a map that gives us tourist attraction places when we search for country.
put(key, value)
/ Step 1: The search term (tourist attraction) data type will be a string // The search result (location) data type will also be a string Map<String, String> tourismMap = new Map<String, String>(); // Step 2: Add search term/result pair tourismMap.put('Egypt','Great Pyramid of Giza'); tourismMap.put('Paris', 'Eiffel Tower'); tourismMap.put('Los Angeles', 'Hollywood Sign'); tourismMap.put('Japan', 'Mount Fuji'); tourismMap.put('London', 'Tower of London'); tourismMap.put('Germany', 'Neuschwanstein Castle'); tourismMap.put('Japan', 'Mount Fuji'); tourismMap.put('India', 'Taj Mahal');
get(key)
This method returns the value of the specified key which is mapped, or it returns null if the map contains no value for this key
// Step 1: The search term (tourist attraction) data type will be a string
// The search result (location) data type will also be a string
Map<String, String> tourismMap = new Map<String, String>();
// Step 2: Add search term/result pair
tourismMap.put('London', 'Tower of London');
tourismMap.put('Germany', 'Neuschwanstein Castle');
tourismMap.put('India', 'Taj Mahal');
// Step 3: Search our map using the get() function
String name = tourismMap.get('Germany');
system.assertEquals('Neuschwanstein Castle', name)
putAll(fromMap)
Copies all of the mappings from the specified map to the original map.
Map<String, String> tourismMap1 = new Map<String, String>();
tourismMap1.put('Egypt','Great Pyramid of Giza');
tourismMap1.put('Paris', 'Eiffel Tower');
tourismMap1.put('Los Angeles', 'Hollywood Sign');
tourismMap1.put('Japan', 'Mount Fuji');
Map<String, String> tourismMap2 = new Map<String, String>();
tourismMap2.put('London', 'Tower of London');
tourismMap2.put('Germany', 'Neuschwanstein Castle');
tourismMap2.put('Japan', 'Mount Fuji');
tourismMap2.put('India', 'Taj Mahal');
// Add map1 entries to map2
tourismMap2.putAll(tourismMap1);
system.debug(tourismMap2.values());
remove(key)
This method removes the mapping for the specified key from the map, if present, and returns the corresponding value.
Map<String, String> tourismMap = new Map<String, String>();
tourismMap.put('Egypt','Great Pyramid of Giza');
tourismMap.put('Paris', 'Eiffel Tower');
tourismMap.put('Los Angeles', 'Hollywood Sign');
//removes the mapping for the specified key from the map
tourismMap.remove('Paris');
system.debug(tourismMap);
values()
This methods returns a list which contains all the values in the map.The values are returned in an arbitrary order.
Map<String, String> tourismMap = new Map<String, String>();
tourismMap.put('Egypt','Great Pyramid of Giza');
tourismMap.put('Paris', 'Eiffel Tower');
tourismMap.put('Los Angeles', 'Hollywood Sign');
//create list to return all the values in the map
List<String> mapValues = new List<String>();
mapValues = tourismMap.values();
system.debug(mapValues);
getSObjectType()
This method returns the value of the sObject type in format of key and value.
Map <id,Account>acctMap = new map<id,account>([select name from Account limit 5]);
for(string accValue:acctmap.keyset()){
system.debug(acctmap.get(accValue));
}
keyset()
Returns a set that contains all of the keys in the map.
Map<String, String> tourismMap = new Map<String, String>();
tourismMap.put('Egypt','Great Pyramid of Giza');
tourismMap.put('Paris', 'Eiffel Tower');
tourismMap.put('Los Angeles', 'Hollywood Sign');
//set to hold keys of map
Set <String> mapKeys = new Set<String>();
mapKeys = tourismMap.keySet();
// return the list of the keys
system.debug(mapKeys);
Map<String, String> tourismMap = new Map<String, String>();
tourismMap.put('Egypt','Great Pyramid of Giza');
tourismMap.put('Paris', 'Eiffel Tower');
tourismMap.put('Los Angeles', 'Hollywood Sign');
Integer mapSize = tourismMap.size();
// return the size of the map
system.debug(mapSize);
Map<String, String> tourismMap = new Map<String, String>();
tourismMap.put('Egypt','Great Pyramid of Giza');
tourismMap.put('Paris', 'Eiffel Tower');
tourismMap.put('Los Angeles', 'Hollywood Sign');
tourismMap.clear();
clone()
Map<String, String> tourismMap = new Map<String, String>();
tourismMap.put('Egypt','Great Pyramid of Giza');
tourismMap.put('Paris', 'Eiffel Tower');
tourismMap.put('Los Angeles', 'Hollywood Sign');
//Makes duplicate copy of the map
Map<String, String> cloneMap = new Map<String, String>();
cloneMap = tourismMap.clone();
system.debug(cloneMap);
containsKey(key)
Returns true if the map contains a mapping for the specified key.
Map<String, String> tourismMap = new Map<String, String>();
tourismMap.put('Egypt','Great Pyramid of Giza');
tourismMap.put('Paris', 'Eiffel Tower');
tourismMap.put('Los Angeles', 'Hollywood Sign');
Boolean contains = tourismMap.containsKey('Paris');
//Returns true if the map contains a mapping for the specified key.
System.assertEquals(true, contains);
hashCode()
Returns the hashcode corresponding to this map.
Map<String, String> tourismMap = new Map<String, String>();
tourismMap.put('Egypt','Great Pyramid of Giza');
tourismMap.put('Paris', 'Eiffel Tower');
tourismMap.put('Los Angeles', 'Hollywood Sign');
//Returns the hashcode corresponding to this map.
Integer storeHashCode = tourismMap.hashCode();
system.debug(storeHashCode);
isEmpty()
Map<String, String> tourismMap = new Map<String, String>();
tourismMap.put('Egypt','Great Pyramid of Giza');
tourismMap.put('Paris', 'Eiffel Tower');
tourismMap.put('Los Angeles', 'Hollywood Sign');
Boolean empty = tourismMap.isEmpty();
//Return true if map has zero key-value pair
System.assertEquals(true, empty);
Scenarios: