Blog

Salesforce Collection of Map

  • (5.0)
  • | 689 Ratings
Salesforce Collection of  Map

Collection

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:

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

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 types, primitive data type (integer, string), Collection, sObject, user-defined.

Features of Map

  • It is the most important type in 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 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('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 = {}

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.

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, '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#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('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.

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.

Arogyalokesh
About The 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.


DMCA.com Protection Status

Close
Close