Blog

Salesforce Collection of Map

  • (4.0)
  • | 36 Ratings |
  • Last Updated February 04, 2019

Collection: When we want to save multiple data of “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.

Same type = all the data are integers or all are strings.
Common business rule = For egage of all employees or salary of all employees.

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

  1. List.
  2. Set.
  3. Map.

Map

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

  • Map is a collection of Key-Value pairs where each unique key maps to a single value.
  • Keys and values can be any data types, primitive data type (integer, string), Collection, sObject, user defined.
Interested in mastering Salesforce? Enroll now for FREE demo on Salesforce Training

Features of Map:

  • It is the most important type in collection.
  • It works on a pair of elements, NEVER AS SINGLE VALUE like 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 key 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 the values. Hence, we use maps instead of LIST.

Lets us look into collection-map with a sample program:

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.

Checkout SalesForce Tutorial

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(&#39;iphone x&#39;, 70000 );
MyMobilePrice.put(&#39;nokia&#39;, 60000 );
MyMobilePrice.put(&#39;samsung&#39;, 50000 );
MyMobilePrice.put(&#39;motorola&#39;, 40000 );

//print the values
system.debug(&#39;My Mobile prices List = &#39; + 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(&#39;iphone x&#39;, 70000 );
MyMobilePrice.put(&#39;nokia&#39;, 60000 );
MyMobilePrice.put(&#39;samsung&#39;, 50000 );
MyMobilePrice.put(&#39;motorola&#39;, 40000 );

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

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

//print the values
system.debug(&#39;Values in the Map = &#39; + 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 = {}

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(&#39;iphone x&#39;, 70000 );
MyMobilePrice.put(&#39;nokia&#39;, 60000 );
MyMobilePrice.put(&#39;samsung&#39;, 50000 );
MyMobilePrice.put(&#39;motorola&#39;, 40000 );

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

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

//print size of the map
system.debug(&#39;The size of the Map = &#39; + 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(&#39;iphone x&#39;, 70000 );
MyMobilePrice.put(&#39;nokia&#39;, 60000 );
MyMobilePrice.put(&#39;samsung&#39;, 50000 );
MyMobilePrice.put(&#39;motorola&#39;, 40000 );

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

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

//print values
system.debug(&#39;The iphone x price is = &#39; + 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

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, &#39;iphone x&#39;);
MyMobilePrice.put(60000,&#39;nokia&#39;);
MyMobilePrice.put(50000 ,&#39;samsung&#39;);
MyMobilePrice.put(40000 , &#39;motorola&#39;);

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

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

//print values
system.debug(&#39;70000 is the price of phone = &#39; + 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(&#39;iphone x&#39;,70000);
MyMobilePrice.put(&#39;nokia&#39;, 60000);
MyMobilePrice.put(&#39;samsung&#39;, 50000);
MyMobilePrice.put(&#39;motorola&#39;, 40000);

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

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

//declaration
Mobiles = MyMobilePrice.keyset();

//print all mobiles list
system.debug(&#39;List of Mobiles = &#39; + Mobiles);</string></string></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(&#39;iphone x&#39;,70000);
MyMobilePrice.put(&#39;nokia&#39;, 60000);
MyMobilePrice.put(&#39;samsung&#39;, 50000);
MyMobilePrice.put(&#39;motorola&#39;, 40000);

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

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

//declaration
Mobiles = MyMobilePrice.values();

//print all mobiles list
system.debug(&#39;List of Mobiles Prices = &#39; + Mobiles);</integer></integer></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#6 - 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(&#39;iphone x&#39;,70000);
MyMobilePrice.put(&#39;nokia&#39;, 60000);
MyMobilePrice.put(&#39;samsung&#39;, 50000);
MyMobilePrice.put(&#39;motorola&#39;, 40000);

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

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

//print status of map check
system.debug(&#39;Is the map empty = &#39; +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 “Apex Class”. Keep following us for more info on Salesforce Development / Programming.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

 

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

About Author

Arogyalokesh

Arogyalokesh takes complex technical ideas and distills them into user-friendly visuals to improve digital marketing campaigns for companies. He is a Content Writer at Mindmajix spreading nuggets of content related to Technology. Proceeded to work in a fast upscale environment, assuring client delight in content writings 


DMCA.com Protection Status