Salesforce Apex Collection: Map

Salesforce Apex Collection: Map

Until now we have discussed from apex collection List and Map.

In this Episode, we are going to discuss the last but one of the very important salesforce apex collection i.e. Map.

So let's get started, 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 type, collections, sObjects, user-defined types, and built-in Apex types).

For example: For every Country(key) there is Currency(value) like for

Country(key) - Currency(value)

United States   - Dollar
Japan              - Yen
France             - Euro
England           - Pound
India               - Rupee
I hope you understood the key-value pair concept, from the above example we can say that whenever I am talking about India automatically it's key i.e. Rupee will be considered as its currency.

Now unlike list and set, there are attributes for Map as well.

Following are the attributes of apex collection Map :

clear() : It removes all of the key-value mappings from the map.

clone() : It makes a duplicate copy of the map.

containskey(object) : It will return true if the map contains a mapping for the specified key.

deepClone() : Makes a duplicate copy of a map, including sObject records if this is a map with sObject record value.

equals(Map) : Compares this Map with the specified map & returns true if both maps are equal. Otherwise, returns false.

get(object) : It returns the value to which the specified key is mapped, or null if the map contains no value for this key.

getsObjectType() : It returns the token of the sObject type that makes up the map values.

hashCode() : It returns the hashcode corresponding to this map.

isEmpty() : It returns true if the map has zero key-value pairs.

keySet() : It returns a set that contains all of the keys in the map.

put(object,object) : It associates the specified value with the specified key in the map.

putAll(map) : It copies all of the mappings from the specified map to the original map.

putAll(sObject []) : It adds the list of sObject records to a map declared as Map<Id,sObject> or Map<string,sObject>.

remove(key) : It removes the mapping for the specified key from the map, if present, and returns the corresponding values.

size() : It returns the no. of key-value pairs in the map.

values() : It returns a list that contains all of the values in the map in arbitrary order.
Now we will understand the syntax of Map :
Set of key : key cannot be duplicate.
List of values : It allows duplicates. 

Syntax : 
Map<Integer,String> myMap = new Map<Integer,String>();
   set<Integer> keys = list<String> values
   void put(key, values)

{1 --> 'sam', 2 --> 'ram', 3 --> 'humar'}

  • sObject get(key) when we give key it will return values.
    string x = m.get(1) -> sam
    string y = m.get(3) -> kumar
  • set<sObject> keyset() : If we use this method we will get set of keys.
    set<Integer> s = m.keySet();  -> {1,2,3}
  • List<sObject> values() : It will return list of values in the map.
    List<string> val = m.values(); -> {'sam', 'ram', 'kumar'}.

Now let's create apex class to demonstrate Map, list, set together.
Let's cook this recipe
Apex Class :
public string mycity{get; set;}
Map<string, List<string>> mybranches = new Map<string,                                                                                                             List<string>>();
public list<selectOption> city {get; set;};
public list<selectOption> branch {get; set;};

public DependExample()
  List<string> sanfrancisco = new List<string>();  //List for city 1
sanfrancisco.add('Alcatraz Island');
  sanfrancisco.add('Golden Gate Bridge');

 List<string> London = new List<string>();  //List for city 2
 London.add('Big Ben');
 London.add('Tower of london');
mybranches.put('sanfrancisco', sanfrancisco);
mybranches.put('London', London);

set<string> keys = mybrances.keyset(); //Set for keys 
city = new list<selectoption>();

branch = new list<selectoption>();
city.add(new selectoption('null','None'));

List<string> my = new List<string>();

for(string a : keys)
   selectoption opi = new selectoption (a,a);  //City with places 

I know it's a little confusing when we are using all apex collection list, set, map together.

But I would suggest you go through my previous apex collection EPISODES to clear and understand list and set.

Coool !! now you know how to use and when we will use Map.


If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you 
Happy learning ☁️⚡️ (Learn. Help. Share.)

<< PREVIOUS                                   NEXT >>                   

APEX COLLECTION: Map APEX COLLECTION: Map Reviewed by on Rating: 5


  1. Anonymous5/01/2019

    Ty for providing great content in an organized way!

  2. I think in containskey method inside the paranthesis value should be "key" not "Object"



Powered by Blogger.