In this session, we will see what is Map class in Apex Salesforce and How to use Map class in Salesforce? Map is a collection of key-value pairs where each unique key maps to a single value. Keys and values in the map collection can
be of any data type such as primitive types, collections, sobject type, user-defined types and built-in apex types.

After completing this tutorial you will be able to understand:

  • What is Map class in Apex Salesforce?
  • How to use Map class in Apex?

What is Map?

Map is a collection of key-value pairs where each unique key maps to a single value. Keys and values in the map collection can be of any data type such as primitive types, collections, sobject type, user-defined types and built-in apex types.

Characteristics of Map:

  • A Map collection keys of string data type are case sensitive that means A Map Collection with keys mykey and MyKey are treated similarly.
  • Values can be retrieved on the basis of keys.
  • Map Collection contains only unique keys but the values can be duplicate.

Syntax for defining a Map:

There are following ways to create a Map object.

Method 1:

Map<T1,T2> map=new Map<T1,T2>();

Above syntax will Create a new instance of the Map class. Where, T1 is the data type of the keys and T2 is the data type of the values.

Example:

Map<Integer, String> map = new Map<Integer, String>();
map.put(1, 'Vinod');
map.put(2, 'Ansh');

Method 2:

Map<T1, T2> map2 = new Map<T1, T2>(maptocopy);

Creates a new instance of the Map class and initializes it by copying the entries from the specified map. T1 is the data type of the keys and T2 is the data type of the values.

Example:

Map<Integer, String> map = new Map<Integer, String>();


map.put(1, 'Vinod');
map.put(2, 'Ansh');
Map<Integer, String> map2 = new Map<Integer, String>(map);
System.debug(map2);

Method 3:

Map<ID,sObject> map=new Map<ID,sObject>(recordList);

Creates a new instance of the Map class and populates it with the passed-in list of sObject records. The keys are populated with the sObject IDs and the values are the sObjects.

Example:

Map<ID, Account> m = new Map<ID, Account>([SELECT Id, Name FROM Account LIMIT 10]);
// After populating the map, iterate through the map entries

for (ID id: m.keyset()) {
Account objAccount = m.get(key);
System.debug(objAccount);
}

Method of Map class:

The following are methods for Map. All are instance methods.

1- clear()

Removes all of the key-value mappings from the map and clears the map object.

2- clone()

Makes a duplicate copy of the map.

3- containsKey(key)

Returns true if the map contains a mapping for the specified key.

4- deepClone():

Makes a duplicate copy of a map, including sObject records if this is a map with sObject record values.

5- equals(map2):

Compares this map with the specified map and returns true if both maps are equal; otherwise, returns false.

6- get(key):

Returns the value to which the specified key is mapped, or null if the map contains no value for this key.

7- getSObjectType():

Returns the token of the sObject type that makes up the map values.

8- hashCode():

Returns the hashcode corresponding to this map.

9- isEmpty():

Returns true if the map has zero key-value pairs.

10- keySet():

Returns a set that contains all of the keys in the map.

11- put(key, value)

Associates the specified value with the specified key in the map.

12- putAll(fromMap):

Copies all of the mappings from the specified map to the original map.

13- putAll(sobjectArray):

Adds the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>.

14- remove(key):

Removes the mapping for the specified key from the map, if present, and returns the corresponding value.

15- size():

Returns the number of key-value pairs in the map.

16- values():

Returns a list that contains all the values in the map.

View More:

Conclusion:

I hope you liked my this tutorial about Map Class in Salesforce. Please don’t hesitate to comment for any technical help. Your feedback and suggestions are always welcome to me.

Thank You.

Leave a Comment