How to use Map methods in Salesforce

Map is a collection of class, which consists a group of elements. It is a key and value pair. Each and every element in the map contains key and value. Key should be unique and value can be duplicated. It supports dynamic memory allocation. It's like a acollection of Key-value pair, where each key(unique) map to a single value. Keys and values can be any data types.

Syntax:

Creating a Map:

      Map<Key,Value> variablename=new Map<Key,Value>();

In salesforce, we have standard object Account, If we want to map id with account, it is represented as,

      Map<Id,Account> Accmap=new Map<Id,Account>();

Different Methods in Map:

put(key, value):

          It returns the value with given key in the map.

Ex:

       Map<String, String> mapname=new Map<String, String>();
          mapname.put('red','shirt');
          mapname.put('blue','tshirt');
          mapname.put('black','shirt');

When the above is executed with both mapname.keyset() and mapname.values() method we will get the values as for keyset:{red, blue, black}
for values:{shirt, tshirt, shirt}.

From this, we can see that values can be duplicated.

If we try to duplicate key, it overrides the existing key and gives the output with the key that is given at last.

clear():

   It removes all the key-value mapping pairs from the map.

get(key):

   It returns the value to which the key is mapped, or it generates null if the key has no value.

keySet():

   It returns the set of keys that contain in the map.

values():

   It returns the set of values that contain in the map.

size():

   It returns the number of key-value pairs in the map.

clone():

   It returns the duplicate copy of the map.

 

 

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Field Update Workflow in Salesforce Raja Gulla

In Salesforce, field update is used to change or update field records. This can be done by a creating field update actions and associating them ...

Continue Reading...
How to create custom list views Raja Gulla

In Salesforce, when you go to the home page of any object, you will get to see a standard view of records in that object. ...

Continue Reading...
How to set up escalation rules in salesforce Raja Gulla

In many businesses, when customers raise cases, and they are assigned manually to the agents by looking over the case details. But nowadays, cases are ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties