You need to sign in to do that
Don't have an account?
Vijay@sfdc
any one share same example using LIST and MAP
Hello All,
i have gone through list and map, would any one share same example using LIST and MAP using 2 object. i am still confused when do i use list and when do i use map? please give me one common example , how MAP reduces code over LIST ?
Thanks,
i have gone through list and map, would any one share same example using LIST and MAP using 2 object. i am still confused when do i use list and when do i use map? please give me one common example , how MAP reduces code over LIST ?
Thanks,
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>();
Please look into below example which will give you an idea about List, Set and Map.
Example:
Find below link for detailed explanation regarding collections:
http://share-salesforce.blogspot.in/2013/05/sfdc-data-collections-listsetmap.html
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_core_concepts.htm
https://developer.salesforce.com/docs/atlas.en- us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm
http://www.sfdc99.com/2013/09/28/data-collections-lists-sets-and-maps/
Please check below post for map method in salesforce
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm
3) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_sets.htm
Please let me know if this helps you.
Regards,
Mahesh
There are to many example are given in developer guide. I will recomend you to download the Apex developer guide .
1) http://amitsalesforce.blogspot.in/search/label/Book
2) https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf
Please check below post for collecttion
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections.htm
Maps
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm
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.
Lists
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm
A list is an ordered collection of elements that are distinguished by their indices. List elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types
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
We can use the Map over list to avoide query inside for loop with above example
Please let us know if this will help you
Thanks
Amit Chaudhary
Amit and mahesh thanks for your reply, could you give us any other highend and more bit more complecte example for using list and maps for looping data from differnt objects.your work is more appreciable
Thanks
1) http://amitsalesforce.blogspot.in/2014/11/dynamic-field-mapping-using-custom.html
Example 1:- How to get Custom setting and put in MAP Example 2:- Descibe call with map
1) http://amitsalesforce.blogspot.in/search/label/Apex%20Describe
Example 3:- Dynamic query
NOTE:- All Above are complex one as per your request.
I will suggest you please download the apex book and try all example.
1) https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf
Please let us know if this will help you
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.
Regards,
Mahesh
Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object.
A map can only contain up to five levels of nested collections inside it.
Map Structure will be like this:
Map<Key, Value>
Key_type represents the primitive type of a map key.
Value_type represents the primitive or sObject type of a map value.
To Understand the use fo map find the below 2 examples,
Example 1:-
Now if the query will return 1000 records then for loop will execute 1000 script statements. Now to avoid this we can simply write:
Example 2:- How to use list in VF page
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_maps_lists.htm Your Visualforce page can show the values like this
Example 3:- Use Maps to navigate across Lists!
http://www.sfdc99.com/2014/01/25/use-maps-navigate-across-lists/
Example 4:-
http://blog.jeffdouglas.com/2011/01/06/fun-with-salesforce-collections/
Thanks
Amit Chaudhary
I think he is not looking for any internet examples, he is looking more of a Realtime scenario.
Please check the realtime scenario which I gave earlier by writing the same requirement using both List and Map.
Please let us know if you need more to understand these concepts.
Please do let me know if it helps you.
Regards,
Mahesh
These posts will cover about list,set,map usage and methods available and how to use map in lightning components,
https://www.sfdc-lightning.com/2018/09/collection-in-salesforce.html
https://www.sfdc-lightning.com/2018/09/how-to-use-map-in-lightning-component.html
(Learn. Help. Share.) - https://www.salesforcekid.com/2019/04/salesforce-apex-collection-list.html
Collections are more helpful when you are dealing with bulk records .
Below post can explains more basics of collections in salesforce .
Salesforce Collections with example (https://salessforcehacks.blogspot.com/2020/01/collections-in-salesforce-list-set-map.html)
Assume maps as dictionary, where you will get all the related words based on the alphabets. In the same way the maps in salesforce works. To avoid the governor limits maps are the important tool especially in triggers.
Differene between maps and lists are:
In lists you have to use for loop, using for loop and iterating all the records just to search for one record or more records that met the criteria is ridiculous, which increases the cpu time.
In maps, we have key and value pair where if you give key, you get the reated value directly without any looping concepts(when you know the key)