You need to sign in to do that
Don't have an account?
Rain.
When to use lists/maps/sets?
Hello
Can someone explain when to use different collection types in Apex with some real-life scenarios?
Some explanation why a certain collection is used would be awesome. (Especially on maps!)
Additionally, are there any collection-specific limitations or best practises which would be good to know?
Thank you!
Can someone explain when to use different collection types in Apex with some real-life scenarios?
Some explanation why a certain collection is used would be awesome. (Especially on maps!)
Additionally, are there any collection-specific limitations or best practises which would be good to know?
Thank you!
Please check this link .If you have any doubt please let us know .
Also one scenario is there you can check ,
http://manojjena20.blogspot.in/2013/05/master-in-apex-collection.html
Let me know if it helps !!
Thanks
Manoj
List,Set,Map are called collections in Apex:
List: A list is an ordered collection
so use list when you want to identify list element based on Index Number.(Lsit can contain Duplicates)
EX: List<Account> accList = new List<Account>();
Set: A set is an unordered collection of primitives or sObjects that do not contain any duplicate elements.
So, use set if you want to make sure that your collection should not contain Duplicates.
EX: Set<Account> accSet = new Set<Account>()
Map: A map is a collection of key-value pairs where each unique key maps to a single value. Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object.
EX: Map<Id, Account> accMap = new Map<Id, Account>();
To Answer your question.
if we need list of account that what we can get like below :- But if base on ID we need to get account detail in that we can use the map
There is a realtime situation where-in we need to loop through the collection of records and get the appropriate value from the matching record.
Requirement is:
Using trigger populate the Account Type on the Contact record (only insert scenario).
To achieve this requirement, here I am mentioning using trigger, so planning to write a trigger:
If we use List and not Map Same Trigger using the Map: If you compare both the triggers,
Trigger 1 is having a List of Accounts, where in we have to loop through the matching Account every time to populate the Acc_Type in the second for loop.
Trigger 2 is having a Map of Accounts with Id and Account as the Datatypes. Hence we can directly get the corresponding Account record and populate the Acc_Type easily.
Hope this will clear you actual doubt.
Please do let me know if it helps you.
Thanks
Amit Chaudhary
List-You can use list when you are dealing with bulk records.
Set-You can use set when you require to deal with Unique records (Set is similar to List collection type ,The only difference is Set does not allow duplicate values ).
Map-Map can help you when you are dealing with parent-child executions .
Please find the below post for basic to advacned examples.
How to use collections in salesforce (https://salessforcehacks.blogspot.com/2020/01/collections-in-salesforce-list-set-map.html)