Salesforce Collection of Map

Salesforce Collection of  Map Module

 

If you would like to become a Salesforce certified professional, then visit Mindmajix - A Global online training platform:  “Salesforce Certification Training”  Course.  This course will help you to achieve excellence in this domain.

 

When we want to save multiple data of “the same type” and also connect with each other with some “common business rule”, then instead of creating multiple variables, we need to create one variable of type collection.

The same type = all the data are integers or all are strings.
Common business rule = For engagement of all employees or the salary of all employees.

Collections are same like an array” - We have 3 types of collections in Salesforce, and they are:

  1. List.
  2. Set.
  3. Map.
In This Blog, You Will Learn

 

[Related Article: Salesforce Tutorial]

Map

The map plays a major role in the collection. We have 2-dimensional data types included in it. Based on the below features, they are widely used in real-time projects.

  • The 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 data type (integer, string), Collection, subject, user-defined.

Features of Map

  • It is the most important type of collection.
  • It works on a pair of elements, NEVER AS SINGLE-VALUE like a set, list.
  • It has a pair in the format of key and value.
  • KEY can’t be duplicate like SET.
  • VALUE can be duplicate like LIST.
  • We can have keys or values of any data type (eg: string, integer, etc).
  • Maps are specially used when we can not refer records easily using meaningless numbers (0,1,2,3,4..., and so on) using LIST. We need something which is related to business to refer to the values. Hence, we use maps instead of LIST.

 MindMajix YouTube Channel

Collection-map with a sample program

Syntax: 

Map My Map = New Map();

Scenario: Add mobile brands with price lists and process them with step-by-step execution with collection functions. The below mentioned are 6 individual programs for adding mobile brand with price list via List.

Program#1 - How to create a Map and save information about Mobile Brand Name and Price?

Scenario: iphone x = 70000, nokia = 60000, samsung = 50000 and Motorola = 40000

// declare a list variable
Map<string, integer=""> MyMobilePrice = New Map<string, integer="">();

//add items
MyMobilePrice.put('iphone x', 70000 );
MyMobilePrice.put('nokia', 60000 );
MyMobilePrice.put('samsung', 50000 );
MyMobilePrice.put('motorola', 40000 );

//print the values
system.debug('My Mobile prices List = ' + MyMobilePrice );</string,></string,>

OutPut:

15:23:25:002 USER_DEBUG [11]|DEBUG|My Mobile prices List = {iphone x=70000, motorola=40000, nokia=60000, samsung=50000}

Program#2 - How to clear all the values in the Map?

// declare a list variable
Map<string, integer=""> MyMobilePrice = New Map<string, integer="">();

//add items
MyMobilePrice.put('iphone x', 70000 );
MyMobilePrice.put('nokia', 60000 );
MyMobilePrice.put('samsung', 50000 );
MyMobilePrice.put('motorola', 40000 );

//print the values
system.debug('My Mobile prices List = ' + MyMobilePrice );

//clear all values in the map
MyMobilePrice.clear();

//print the values
system.debug('Values in the Map = ' + MyMobilePrice );</string,></string,>

OutPut:

15:28:03:002 USER_DEBUG [11]|DEBUG|My Mobile prices List = {iphone x=70000, motorola=40000, nokia=60000, samsung=50000}
15:28:03:002 USER_DEBUG [17]|DEBUG|Values in the Map = {}

[Related Article: Salesforce Interview Questions]

Program#3 - How to get the size of the Map?

// declare a list variable
Map<string, integer=""> MyMobilePrice = New Map<string, integer="">();

//add items
MyMobilePrice.put('iphone x', 70000 );
MyMobilePrice.put('nokia', 60000 );
MyMobilePrice.put('samsung', 50000 );
MyMobilePrice.put('motorola', 40000 );

//print the values
system.debug('My Mobile prices List = ' + MyMobilePrice );

//size of the map
Integer length = MyMobilePrice.size();

//print size of the map
system.debug('The size of the Map = ' + length );</string,></string,>

OutPut:

15:39:35:002 USER_DEBUG [11]|DEBUG|My Mobile prices List = {iphone x=70000, motorola=40000, nokia=60000, samsung=50000}
15:39:35:002 USER_DEBUG [17]|DEBUG|The size of the Map = 4

Program#4 - How to get an element by passing KEY?

// declare a list variable
Map<string, integer=""> MyMobilePrice = New Map<string, integer="">();

//add items
MyMobilePrice.put('iphone x', 70000 );
MyMobilePrice.put('nokia', 60000 );
MyMobilePrice.put('samsung', 50000 );
MyMobilePrice.put('motorola', 40000 );

//print the values
system.debug('My Mobile prices List = ' + MyMobilePrice );

//get element by passing KEY
integer PhoneModel = MyMobilePrice.get('iphone x');

//print values
system.debug('The iphone x price is = ' + PhoneModel );</string,></string,>

OutPut:

15:58:24:002 USER_DEBUG [11]|DEBUG|My Mobile prices List = {iphone x=70000, motorola=40000, nokia=60000, samsung=50000}
16:45:48:002 USER_DEBUG [17]|DEBUG|The iphone x price is = 70000

Note: Based on the KEY value passed during the syntax declaration, the output varies. We can prove that the below sample program is a reverse scenario for the above program.

Program#4(1) - How to get an element by passing KEY?

// declare a list variable
Map<integer, string=""> MyMobilePrice = New Map<integer, string="">();

//add items
MyMobilePrice.put(70000, 'iphone x');
MyMobilePrice.put(60000,'nokia');
MyMobilePrice.put(50000 ,'samsung');
MyMobilePrice.put(40000 , 'motorola');

//print the values
system.debug('My Mobile prices List = ' + MyMobilePrice );

//get element by passing KEY
string PhoneModel = MyMobilePrice.get(70000);

//print values
system.debug('70000 is the price of phone = ' + PhoneModel );</integer,></integer,>

OutPut:

16:13:35:002 USER_DEBUG [11]|DEBUG|My Mobile prices List = {40000=motorola, 50000=samsung, 60000=nokia, 70000=iphone x}
16:13:35:002 USER_DEBUG [17]|DEBUG|70000 is the price of phone = iphone x.

Program#5 - How to get all KEYS together?

// declare a list variable
Map<string, integer=""> MyMobilePrice = New Map<string, integer="">();

//add items
MyMobilePrice.put('iphone x',70000);
MyMobilePrice.put('nokia', 60000);
MyMobilePrice.put('samsung', 50000);
MyMobilePrice.put('motorola', 40000);

//print the values
system.debug('My Mobile prices List = ' + MyMobilePrice );

//collect all keys together
set Mobiles = new set();

//declaration
Mobiles = MyMobilePrice.keyset();

//print all mobiles list
system.debug('List of Mobiles = ' + Mobiles);</string,></string,>

OutPut:

16:43:19:002 USER_DEBUG [11]|DEBUG|My Mobile prices List = {iphone x=70000, motorola=40000, nokia=60000, samsung=50000}
16:43:19:002 USER_DEBUG [20]|DEBUG|List of Mobiles = {iphone x, motorola, nokia, samsung}

Program#6 - How to get all VALUES together?

// declare a list variable
Map<string, integer=""> MyMobilePrice = New Map<string, integer="">();

//add items
MyMobilePrice.put('iphone x',70000);
MyMobilePrice.put('nokia', 60000);
MyMobilePrice.put('samsung', 50000);
MyMobilePrice.put('motorola', 40000);

//print the values
system.debug('My Mobile prices List = ' + MyMobilePrice );

//collect all keys together
list Mobiles = new list();

//declaration
Mobiles = MyMobilePrice.values();

//print all mobiles list
system.debug('List of Mobiles Prices = ' + Mobiles);</string,></string,>

OutPut:

16:54:06:002 USER_DEBUG [11]|DEBUG|My Mobile prices List = {iphone x=70000, motorola=40000, nokia=60000, samsung=50000}
16:54:06:002 USER_DEBUG [20]|DEBUG|List of Mobiles Prices = (70000, 60000, 50000, 40000)

Program#7 - How to check if the map is empty or not?

// declare a list variable
Map<string, integer=""> MyMobilePrice = New Map<string, integer="">();

//add items
MyMobilePrice.put('iphone x',70000);
MyMobilePrice.put('nokia', 60000);
MyMobilePrice.put('samsung', 50000);
MyMobilePrice.put('motorola', 40000);

//print the values
system.debug('My Mobile prices List = ' + MyMobilePrice );

//check map is empty
Boolean CheckList = MyMobilePrice.isempty();

//print status of map check
system.debug('Is the map empty = ' +CheckList );</string,></string,>

OutPut:

16:59:28:002 USER_DEBUG [11]|DEBUG|My Mobile prices List = {iphone x=70000, motorola=40000, nokia=60000, samsung=50000}
16:59:28:002 USER_DEBUG [17]|DEBUG|Is the map empty = false

Assignment: Create Map, print every value for each loop. 

Course Schedule
NameDates
Salesforce TrainingOct 12 to Oct 27View Details
Salesforce TrainingOct 15 to Oct 30View Details
Salesforce TrainingOct 19 to Nov 03View Details
Salesforce TrainingOct 22 to Nov 06View Details
Last updated: 03 Apr 2023
About Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.

read less
  1. Share:
Salesforce Articles