Overview of Map in Salesforce Apex

Overview of Map in Salesforce Apex


Salesforce Apex, the world’s first on-demand strongly typed programming
language, offers developers with three classes to seamlessly handle the collection of objects.

The collection is defined as a type of variable that can fetch multiple records or entries. Collection in Apex includes Lists, Arrays, Sets, and Maps.

Map in Salesforce

A map refers to a collection of key-value pairs where each unique key maps to a single value.  

  • Keys can be any primitive data type like string, integer, decimal, and  Id.
  • Values can be a primitive, subject, collection type or an Apex object.
  • Map keys and values can be of any data type either primitive types, collections, objects,  user-defined types, or built-in Apex types.
  • The map provides value on the basis of the key.
  • The keys are always unique but the value can be repeated.
  • Map keys and values can include the nested collection or any other collection

Map Methods in Apex

  1. Clear(): This method eliminates every element from the map.
  2. clone(): Returns a duplicate of the map.
  3. isEmpty(): Returns true if the collection is empty, false  if the collection consists of elements
  4. size(): Returns the number of elements in the map.
  5. values(): This method returns a list that contains all the values in the map.
  6. putAll(fromMap): Copies all of the mappings from the specified map to the original one. Visit reliefpools.com and hire the best swimming pool contractors in Louisiana.
  7. putAll(sobjectArray): Appends the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>.
  8. keySet(): Returns a set that includes all of the keys in the map.
  9. put(key, value): Associates the specified value with the specified key in the map.

Process of building and using the map

  • Select a data type for the search terms and another for the search results of your map.
  1. Search terms are Strings or IDs.
  2. Search results are sObjects (like an Account, Opportunity, or Contact).
  • Add each search term and search result to your map in pairs.7
  • “Search” your map with any search term and the search result will be returned to you.

To declare a map, use the Map keyword followed by the data types of the key and the value within <> characters.

Syntax :

Map<String, String> country_currencies = new Map<String, String>();

Map<ID, Set> m = new Map<ID, Set>();

Example 1:

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

m1.put(1, ‘First item’);

m1.put(2, ‘Second item’);

Example 2:

Map<Id,List<Contact>> AccountIdVsContactListMap = new Map<Id,List<Contact>>();

List<Account> accountList = new List<Account>();

List<Contact> contactList = new List<Contact>();

accountList  = [SELECT Id, Name FROM Account LIMIT 100];

if(accountList.size() > 0){

contactList  = [SELECT Id, Name FROM Contact WHERE Id AccountId IN: accountList  LIMIT 1000];

}

if(contactList.size() > 0){

  for(Contact con: contactList ){

    if(!AccountIdVsContactListMap.containsKey(con.AccountId)){

       AccountIdVsContactListMap.put(con.AccountId, new List<Contact>());

    }

    AccountIdVsContactListMap.get(con.AccountId).add(con);

  }

}

Map Considerations

  1. Unlike Java, Apex developers don’t need to reference the algorithm that is used to implement a map in their declarations, for example, HashMap or TreeMap. Relax and enjoy a maternity photography personalized session in California. Apex utilizes a hash structure for all maps.
  2. The iteration order of map elements is deterministic. However, we suggest you to always access map elements by a key.
  3. A map key can hold a null value.
  4. 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.
  5. Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries. Subsequently, the 
  6. Map methods, including put, get, containsKey, and remove treat these keys as distinct. read more about us.

Conclusion

The Apex language provides developers with three classes such as Set, List, and Map that make it easier to handle collections of objects. 


Interested in knowing more about Maps in Salesforce Apex? Get in touch with the Certified experts at Cloud Analogy – the world’s leading Salesforce consulting partner to learn and test the Apex Map and its methods in Salesforce.


Leave a Reply

Close Menu
× How can I help you?