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:
In This Blog, You Will Learn |
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.
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.
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}
// 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]
// 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
// 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.
// 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.
// 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}
// 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)
// 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.
Name | Dates | |
---|---|---|
Salesforce Training | Oct 12 to Oct 27 | View Details |
Salesforce Training | Oct 15 to Oct 30 | View Details |
Salesforce Training | Oct 19 to Nov 03 | View Details |
Salesforce Training | Oct 22 to Nov 06 | View Details |
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.