function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
himanshu huske 7himanshu huske 7 

Set and Maps

Hi guys help me out with some concept,
I learnt Set, Map and all their releted methods.
but while writing triggers I cannot decide when to use Map and Set,
please tell me how to decide, while writing, now their is need of Map.
Salesforce seekarSalesforce seekar
Hello Himanshu , 
i am just giving an idea briefly , if you practice it you will understand clearly .
set : if you want to add unique things then go for set. here it will not allow duplicates 
Map: basically we use maps to handle bulk data scenarios , as it has key ,value pair , using key we will perform the required logic . 

br 
Salesforce Seekar 
Pallavi KavarePallavi Kavare
Hi Himanshu, 
In context of trigger one can use set to store the I'd of new or old records which we get using trigger.new or trigger.old to do further processing on those unique set of records. 
Map can be used when you have duplicate values but key will be unique and need further implementation based on that. 
Ajay K DubediAjay K Dubedi

Hi Himanshu,

Greetings to you!

You can find the difference between set, list, and map below.

1) If you need to access elements frequently by using the index then List is a way to go. 

2) If you want to store elements and want them to maintain an order in which they are inserted into a collection then go for List again, as List is an ordered collection and maintain insertion order.

3) If you want to create a collection of unique elements and don't want any duplicate then choose any Set implementation 

All Set implementation follow there general contract e.g. uniqueness 

4) If you store data in the form of key and value than Map is the way to go. 

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
 
Deepali KulshresthaDeepali Kulshrestha
Hi himanshu,
Greetings to you!

Please refer to these links: explain map and set .


https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm
https://www.forcetalks.com/salesforce-topic/difference-between-listset-and-map-in-salesforce/
https://www.janbasktraining.com/blog/map-in-salesforce/
http://sfdcinpractice.com/index.php/2016/12/18/3-5-use-map-bulkify-code/

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.


Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com