I have been studying type of collection in salesforce. Can you explain what exactly is Map in the types of Collections in the best possible way? I have some articles but I still cant understand it clearly. Thanks in advance.

  • if you find my answer useful then please appreciate it.
    – Ajay Gupta
    Nov 2 '15 at 18:28

A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types. For example if you have some country data as follow

Key        value

IND        India

US         USA

AUS        Australia

CHI        CHINA

it can be added to a map in following way

Map<String,String> countryMap = new Map<String,String>();

once it is defined in way you can get value of any country code with out looping through whole collection of items as we generally do with List type collection.


above code will return China as result.

Map collection is quite useful while working in trigger to store sObject data.


Map is Collection which contains Key-Value pair. Each element has a key and corresponding value.


Map <<"datatypeofkey, datatypeofvalue">> var=new Map <<"datatypeofkey, datatypeofvalue">>();

Key and value support almost all data type. below are some interesting methods available to play on map and will tell why using map is beneficial:

Map m = new Map{5 => 'Jon', 6 => 'Quinton', 1 => 'Reid'};  
// displays all keys
System.debug('=== all keys in the map: ' + m.keySet());  
// displays all values
System.debug('=== all values in the map (as a List): ' + m.values());  
// does the key exist?
System.debug('=== does key 6 exist?: ' + m.containsKey(6));  
// fetches the value for the key
System.debug('=== value for key 6: ' + m.get(6));  
// adds a new key/value pair
// returns the number of elements
System.debug('=== size after adding Dave: ' + m.size());  
// removes an element
System.debug('=== size after removing Jon: ' + m.size());  
// clones the map
Map m1 = m.clone();  
System.debug('=== cloned m1: ' + m1);  
// removes all elements
// returns true if zero elements
System.debug('=== is m empty? ' + m.isEmpty()); 

One more important use of map is you can create map directly from query:

Map<Id, Account> m = new Map<Id, Account>([Select name from Account]);



What is MAP?

A Map is a collection of key-value pairs. Keys can be any primitive data type while values can include primitives, Apex objects, sObjects and other collections. Use a map when you want to quickly find something by a key. Each key must be unique but you can have duplicate values in your Map.

Short Description:

Map holds key and value pair in simple terms.


Map<datatype,datatype> mapName = new Map<datatype,datatype>();

Methods Available in MAP:

keyset(): To retrive only keys from the map.
values(): To retrive only values from the map.
containsKey(value): To find a key from the map.
get(key): By passing the key we can retrive the value.
put(key,value): To add new key and value in a map.

Maximum Size:

No limit for the size of a MAP. It only depends on the heap size which is 6 MB (Synchronous) and 12 MB (Asynchronous).


Map<string,string> countryISTCodeMap = new Map<string,string>();
countryISTCodeMap.put('India','911');//replaces old value with new value.

Map<string,string> countryISTCodeMap = new Map<string,string>{'India'=>'91','USA'=>'001', 'India'=>'911'};

Note : Map keys are case-sensitive. Hence key value India and india are treated as different keys in map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.