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 engage 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:
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 list 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 = {}
// 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.
Checkout Salesforce Interview Questions
// 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 by for each loop.
In the next topic, we will discuss in detail about “How to call APEX Class in Salesforce”. Keep following us for more info on Salesforce Development / Programming.
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts
Free Demo for Corporate & Online Trainings.