Today's Offer - Salesforce Certification Training - Enroll at Flat 10% Off.

- Salesforce Blogs -

Map In salesforce

The Map methods are all instance methods, that is, they operate on a particular instance of a map. The following are the instance methods for maps What is map?

  • A map is a collection of key-value pairs where each unique key maps to a single value. In map Keys can be any primitive data type like string, integer,decimal, Id while values can be a primitive, sObject, collection type or an Apex object.
  • Map keys and values can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
  • Map keys of type String are case-sensitive. Map methods, including put, get, containsKey, and remove treat these keys as distinct.
  • Map gives value on basis of key.
  • Key always unique but value can be duplicated.


Map<integer, string>exMap= new map<integer, string>();
string name= exMap.get(1);
string name1= exMap.get(2);
string name2= exMap.get(3);
system.debug(name2);// Peter
set<integer>key= exmap.keyset();
system.debug(values);//James, Daniel, Peter

salesforce Curriculum Map Methods

Read: How to Integrate Salesforce with Outlook, Gmail, & Mailchimp

The following are methods for Map. All are instance methods.

  • clear() Removes all of the key-value mappings from the map.
  • clone() Makes a duplicate copy of the map.
  • containsKey(key) This method returns true if the map contains  the  If the key is a string, the key value is case-sensitive.
  • deepClone() DeepClone () method creates a duplicate copy of a map, including sObject records if the map has sObject record values .
  • equals(map2) It compares a map with a specified map and returns true if both maps are equal; otherwise, returns false.
  • 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.
  • getSObjectType() This method returns the value of the sObject type in format of key and value.
  • isEmpty() If map has zero key-value pairs it returns true.
  • keySet() Returns a set that contains all of the keys in the map.
  • put(key, value):
  • Associates the specified value with the specified key in the map.
  • putAll(fromMap) Copies all of the mappings from the specified map to the original map.
  • putAll(sobjectArray) Adds the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>.
  • remove(key) This method removes the mapping for the specified key from the map, if present, and returns the corresponding value.
  • size() Returns the number of key-value pairs in the map.
  • values() This methods returns a list which contains all the values in the map.The values are returned in an arbitrary order.

salesforce quiz get(key):

list<account>myAccounts = new list<account>();
myAccounts = [Select ID, Name from Account limit 10]; 
map<id,string>myAMap = new map<id,string>();
for ( Account a : myAccounts ){
//Here putting account Id and name to map
myAMap.put(a.ID, a.Name);
for ( ID aID : myAMap.keySet() ){
system.debug(loggingLevel.debug, myAMap.get(aID));

Maps Class and Objects of Apex Salesforce putAll(fromMap):

Read: What Is Salesforce Chatter And Its Benefits?

Map<String, String> map1 = new Map<String, String>();
Map<String, String> map2 = new Map<String, String>();
// Add map1 entries to map2
System.debug(map2.values());//(LightRed, 'DarkRed')


Map<String, String>colorCodes =   new Map<String, String>();
colorCodes.put('Red', 'FF0000');
colorCodes.put('Blue', '0000A0');
colorCodes.put('Green', '0000A0');
System.debug('printing=== '+colorCodes);// Blue=0000A0, Green=0000A0, Red=FF0000
for(String key:colorCodes.keySet().clone()) {
system.debug(colorCodes);// Blue=0000A0, Red=FF0000

Values ():

Map<String, String>colorCodes = new Map<String, String>();
colorCodes.put('FF0000', 'Red');
colorCodes.put('0000A0', 'Blue');
List<String> colors = new List<String>();
colors = colorCodes.values();
system.debug(colors);//(Red, Blue)


Map <id,Account>acctMap = new map<id,account>([select  name from  Account limit 10]);
for(string accValue:acctmap.keyset()){

Maps Class and Objects of Apex Salesforce Example How to display record on basis of alphabet

Apex class
public class DynamicSearchExample {
public Map<string,list<account>>accountsMap {get; set;}
public List<selectoption> keys {get; set;}
public String selectedKey {get; set;}
public Map<string, account>accsByName {get; set;}
public Set<string>getMapKeys(){
accsByName=new Map<string, account>();
        List<string>sortedKeys=new List<string>();
accountsMap=new Map<string, list<account>>();
accountsMap.put('All', new List<account>());
        List<account>accs=[select Name, industry,type,phone from Account order by Name asc];               
for (Account acc : accs){
            String start=acc.Name.substring(0,1);
if (null==accsFromMap){
accsFromMap=new List<account>();
keys=new List<selectoption>();
for (String key : accountsMap.keySet()){
if(key != 'All'){
for (String key : sortedKeys){
keys.add(new SelectOption(key, key));
Visualforce page
<apex:page controller="DynamicSearchExample">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%0Afunctionredraw_accounts()%7B%0A%0A%20%20%20%20%7D%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />
<apex:actionFunction name="redraw_accounts" rerender="accs" status="status"/>
<apex:pageBlock title="Criteria">
<apex:outputLabel value="Starting Letter"/>
<apex:selectList value="{!selectedKey}" size="1" onchange="redraw_accounts()">
<apex:selectOptions value="{!keys}" />
<apex:pageBlock title="Accounts">
<apex:actionstatus id="status" startText="Loading Data..........." stopText="Loading completed....." >
<apex:facet name="start"/>
<apex:facet name="stop">
<apex:outputPanel id="accs">
<apex:pageBlockTable value="{!accountsMap[selectedKey]}" var="acc">
<apex:column value="{!}"/>
<apex:column value="{!acc.industry}"/>
<apex:column value="{!acc.type}"/>
<apex:column value="{!}"/>

Output Maps Class and Objects of Apex Salesforce

Read: What is Salesforce Field Service Lightning and Why is it So Popular?

    Janbask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.

Trending Courses


  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

0 day 22 Jan 2020


  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

5 days 27 Jan 2020

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

2 days 24 Jan 2020


  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

3 days 25 Jan 2020


  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

6 days 28 Jan 2020


  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

5 days 27 Jan 2020

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

5 days 27 Jan 2020

SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

9 days 31 Jan 2020


Search Posts


Receive Latest Materials and Offers on Salesforce Course