Apex Map In SalesForce

Reading this article, you can learn and test the Apex Map and its methods in SalesForce.

About Salesforce

 
Salesforce is a cloud-based online solution for customer relationship management or CRM. Salesforce provides all of our departments like marketing, sales, commerce, and service with a shared view of our customers with a single integrated CRM platform.
 
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on Salesforce servers in conjunction with calls to the API. Using syntax that looks like Java and acts like database stored procedures, Apex enables developers to add business logic to most system events, including button clicks, related record updates, and Visualforce pages. Apex code can be initiated by Web service requests and from triggers on objects. – Salesforce.
 
Apex Collections is a type of variable, it can store multiple records. The different types of Collections are, List, Set, and Map.
 
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.
 
Reading this article, you can learn and test the Apex Map and its Methods in SalesForce.
 
The prerequisites for testing Apex Map in SalesForce is the following -
 
Register a Salesforce Free Trial account using the following link.
 
Step 1
 
Log into your Salesforce account and click the "Developer Console".
 
Apex Map In SalesForce
 
Step 2
 
The general syntax for Apex Map is,
 
Map<Key,Value> variablename=new Map<Key,Value>();
 
For learning more about Apex DataType, please refer to this link.
 
Now, we can create a new Apex Class named ApexMap and the file named as ApexMap.apxc.
 
Apex Map In SalesForce
 
Apex Map In SalesForce
 
After creating the Apex class ApexMap, add a Maptest method for creating and testing the map and its methods.
 
Apex Map In SalesForce
 
Step 3
 
Next, we can add some predefined important methods like put(key, value), get(key), keySet(), values(), size(), clone(), and clear() to the Apex Map.
 
put(key, value) – Associates the specified value with the specified key in the map. The code is,
  1. Map<String, String> studDept=new Map<String, String>();  
  2. studDept.put('CSE','Kavin');  
  3. system.debug(studDept);  
get(key) - using this method, we can get the value to which the key is mapped, or it generates null if the key has no value.
  1. studDept.put('D02','CIVIL');  
  2. studDept.put('D03','MECH');  
  3. studDept.put('D04','EEE');  
  4. system.debug('get(D01) is: '+ studDept.get('D01'));  
keySet()- using this method, we can get the set of keys that contain in the map.
  1. system.debug('Department keyset() are : '+ studDept.keyset());  
values() - using this method, we can the set of values that contain in the map.
  1. system.debug('Department values() are : '+ studDept.values());  
size() - using this method, we can get the total number of key-value pairs in the Map.
  1. Integer s=studDept.size();  
  2. system.debug('size() - Department Map Size : '+s);  
clone() - using this method, we can get the duplicate copy of the map.
  1. Map<String, String> studDeptc=studDept.clone();  
  2. system.debug('clone() - Department values() are : '+ studDeptc.values());  
clear() - using this method, we can removes all the key-value mapping pairs from the map.
  1. studDeptc.clear();  
  2. system.debug('clear() - Department values() are : '+ studDeptc.values());  
Finally, the ApexMap.apxc class code is mentioned below.
  1. public class ApexMap {  
  2.     public static void Maptest() {  
  3.         Map < String, String > studDept = new Map < String, String > ();  
  4.         //Put  
  5.         studDept.put('D01''CSE');  
  6.         system.debug(studDept);  
  7.         //get(key)  
  8.         studDept.put('D02''CIVIL');  
  9.         studDept.put('D03''MECH');  
  10.         studDept.put('D04''EEE');  
  11.         system.debug('get(D01) - Department is: ' + studDept.get('D01'));  
  12.         // keySet()  
  13.         system.debug('Department keyset() are : ' + studDept.keyset());  
  14.         // values()  
  15.         system.debug('Department values() are : ' + studDept.values());  
  16.         //Size()  
  17.         Integer s = studDept.size();  
  18.         system.debug('size() - Department Map Size : ' + s);  
  19.         //clone()  
  20.         Map < String, String > studDeptc = studDept.clone();  
  21.         system.debug('clone() - Department values() are : ' + studDeptc.values());  
  22.         //clear  
  23.         studDeptc.clear();  
  24.         system.debug('clear() - Department values() are : ' + studDeptc.values());  
  25.     }  
  26. }  
Step 4
 
For debugging the Apex class ApexMap, click the Debug menu and select "Open Execute Anonymous Window", as shown below.
 
Apex Map In SalesForce
 
Now, write the Apex code for calling the Maptest() method, and enable the "Open log" option for viewing output and click "Execute".
 
Apex Map In SalesForce
 
ApexMap.Maptest();
 
Step 4
 
Now, we can verify the output. After clicking Execute, the log will open automatically. Select "Debug Only".
 
.Apex Map In SalesForce
 

Summary

 
Now, you have successfully tested the Apex Map and its methods in SalesForce environment.