Map:
Map Syntax:
Map Methods
PUT Method: Adds pairs to the map or associates value to a specific key in a map.
You May Also Like: What Is a List In Apex?
Get Method: Allows us to retrieve keyvalues from a map using the key.
Remove Method: Retrieves element from a Map.
Clear Method: Removes all the elements from the Map and sets the size of the Set to Zero.
Clone Method: Creates a new Map and adds all the elements of an existing Map into that new Map.
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.
ContainsKey Method: Boolean method which helps us to check if a key is present in a map.
IsEmpty Method: Boolean method which returns true if the Map has no elements.
Size Method: Return the number of the elements stored in the Set.
keySet Method: Returns a set or list that contains all of the keys in the map.
Values Method: Returns a list that contains all of the values in the map in the arbitrary order.
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?
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);
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
// 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();