What Is a Map In Apex?

Map:
apex_collections
         A map is a collection of key-value pair values. Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object. Keys have to be unique in the collection where as values can be duplicate.

Also Read: What Is Collections In Apex?

The following table represents a map of Employee Id’s and Employee Names

121
127
657
985
111
123
‘Nagarjun’
‘Hari’
‘Krishna’
‘Praveen’
‘Mahi’
‘Malli’

Map Syntax:
// Declaring Map
Map<Keydatatype,Valuedatatype> mapname;

// Creating a new map
Map<Keydatatype,Valuedatatype> mapname = New Map<Keydatatype,Valuedatatype>();

Map Methods
PUT Method: Adds pairs to the map or associates value to a specific key in a map.
// Add to map 
Map<Integer, String> Empmap = Map<Integer,String>();
Empmap.put(121,'Hari');
Empmap.put(122,'Krishna');

// Add s-Object wiht id to Map
Map<Id, Account> Accmap = Map<Id,Account>();
Empmap.put(acc1id,acc1);
Empmap.put(acc2id,acc2);

You May Also Like: What Is a List In Apex?

Get Method: Allows us to retrieve keyvalues from a map using the key.
// Get Elements from map
Map<Integer, String> Empmap = Map<Integer,String>();
Empmap.put(121,'Hari');
Empmap.put(122,'Krishna');
String s = Empmap.get('121') 
String s = Empmap.get('122')

Remove Method: Retrieves element from a Map.
// Remove Element From Map
Map<Integer, String> Empmap = Map<Integer,String>();
Empmap.put(121,'Hari');
Empmap.put(122,'Krishna');
Empmap.remove(121);

Clear Method: Removes all the elements from the Map and sets the size of the Set to Zero.
// clear the Elements From Map
Map<Integer, String> Empmap = Map<Integer,String>();
Empmap.put(121,'Hari');
Empmap.put(122,'Krishna');
Empmap.clear();

Clone Method: Creates a new Map and adds all the elements of an existing Map into that new Map.
// Cloning the map
Map<Id,Account> Accmap = New Map<Id,Account>();
Accmap.put(id1,'Hari');
Accmap.put(id2,'Krishna');
Map<Id, Account> map2 = Emp1map.clone();

Note: If this is a map with sObject record values, the duplicate map will only be a shallow copy of the map. That is, the duplicate will have references to each sObject record, but the records themselves are not duplicated.

You Might Also Like: What Is The Difference Between Shallow And Deep Clone?

Deepclone Method: Creates a new Map and adds all the elements of an existing Map physically into that new Map.
// Example 1
Map<Id,Account> Accmap = New Map<Id,Account>();
Accmap.put(id1,'Hari');
Accmap.put(id2,'Krishna');
Map<Id, Account> map2 = Emp1map.clone(); 

//Example 2
Map<Integer,Account> Accmap = New Map< Integer,Account>();
Accmap.put(121,'Hari');
Accmap.put(122,'Krishna');
Map<Id, Account> map2 = Emp1map.cloneclone();

ContainsKey Method: Boolean method which helps us to check if a key is present in a map.
// Check the Elements in Map
Map<Integer,Account> Accmap = New Map< Integer,Account>();
Accmap.put(121,'Hari');
Accmap.put(122,'Krishna');
Boolean contains = Accmap.containsKey(121);

IsEmpty Method: Boolean method which returns true if the Map has no elements.
// Check the Map empty
Map<Integer,Account> Accmap = New Map< Integer,Account>();
Accmap.put(121,'Hari');
Boolean isitempty = Accmap.isEmpty();
system.debug('The map is Empty' + isitempty);
Result:- 13:32:49:023 USER_DEBUG [3]|DEBUG|The map is Empty False

Size Method: Return the number of the elements stored in the Set.
// Check the Size of the Map
Map<Integer,Account> Accmap = New Map< Integer,Account>();
Accmap.put(121,'Hari');
Accmap.put(122,'Krishna');
Integer accmapsize = Accmap.Size();
system.debug('The map size is' + accmapsize);
Result:- 13:32:49:023 USER_DEBUG [3]|DEBUG|The map size is 2

keySet Method: Returns a set or list that contains all of the keys in the map.
// Set the key to Map
Map<Integer,Account> Accmap = New Map< Integer,Account>();
Accmap.put(121,'Hari');
Accmap.put(122,'Krishna');
Set<Integer> acctnums = Accmap.keySet();

Values Method: Returns a list that contains all of the values in the map in the arbitrary order.
// Return the map values 
Map<Integer,Name> Empmap = New Map< Integer,Account>();
Empmap.put(121,'hari');
Empmap.put(122,'Krishna');
List<String> Empnames = Empmap.values();

COMMENTS

Name

Apex Apex Default Methods Apex Methods Apex_Collections Apps Batch Apex Books CRM eBooks Data Import Wizard Data Management DataLoader Dataloader.io Difference Between Error in Salesforce FAQ's FAQ's-Apex FAQ's-Apps FAQ's-CRM FAQ's-Data loader FAQ's-SOQL & SOSL FAQ'S-Triggers FAQ's-User Profile & Security FAQ's-VF Force.com Explorer Force.com Ide Formulas & Functions Integration Interview Questions Only Latest_Updates Limits&Best Practices Online-Training Reports and Dashboards Salesforce Deployment Salesforce Realtime Examples On Development Salesforce Realtime Task On Admin Salesforce Realtime Tasks Salesforce Realtime Tasks- Apex Salesforce Training Salesforce1 Mobile Sites SOQL Spring'14 Release Triggers User Profile & Security VF Tags VisualForce Winter'14 Release Wizard for Accounts/Contacts Workbench Workflows and Approvals
false
ltr
item
Sfdc Gurukul- All in one place for salesforce and force.com step by step tutorial for beginners: What Is a Map In Apex?
What Is a Map In Apex?
What Is a Map In Apex?,How to use map in Apex,apex maps to using in controllers,using map collection in apex programming
http://3.bp.blogspot.com/-87o7fxVdrEg/Uzmec-0wW0I/AAAAAAAABpQ/BpyIf8oJ5sQ/s1600/apex_collections.jpg
http://3.bp.blogspot.com/-87o7fxVdrEg/Uzmec-0wW0I/AAAAAAAABpQ/BpyIf8oJ5sQ/s72-c/apex_collections.jpg
Sfdc Gurukul- All in one place for salesforce and force.com step by step tutorial for beginners
http://sfdcgurukul.blogspot.in/2013/07/what-is-map-in-apex.html
http://sfdcgurukul.blogspot.in/
http://sfdcgurukul.blogspot.com/
http://sfdcgurukul.blogspot.com/2013/07/what-is-map-in-apex.html
true
4199533888133360731
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy