+ Start a Discussion
AniltAnilt 

When do we use set, map, list

Can any one please let me know when do we use list? when do we use map? when do we use set?

 

I'm bit confused regarding their usage.

Best Answer chosen by Admin (Salesforce Developers) 
HariDineshHariDinesh

Hi,

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>

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>

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. For example, the following table represents a map of countries and currencies


Find below link for Detailed explanation regarding collections
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections.htm

 

:

 

All Answers

HariDineshHariDinesh

Hi,

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>

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>

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. For example, the following table represents a map of countries and currencies


Find below link for Detailed explanation regarding collections
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections.htm

 

:

 

This was selected as the best answer
AniltAnilt

Thanks HariDinesh

AniltAnilt

If I have

 

Map<String, Lead>  and let us assume that string is email (one of the field in lead)..what happens when we use that code...like email will have a relation with lead? (as we all know that email is already related with the  lead)

 

how do we pronounce it clearly...like whether lead has a relation with string or mapped with string...

HariDineshHariDinesh

Hi,,

 

In Map, Generally we called as Key-Value pair.

 

As in your Example:  Map<email,Lead>

 

Then we called as : The value of Email1 is Lead1

                                      and

                                    Lead1 has Key as Email1

AniltAnilt

So here Lead1 is 1st record..thats what you are telling? please let me know if I'm worng

 

So if 1st email is mapped with 1st record, 2nd email is mapped with 2nd record and so on..

 

Please let me know whether we are on the same page.

HariDineshHariDinesh

Yes,

 

Exactly..

sforceBangaloresforceBangalore

i think this can help you too...though like sharing

 

http://share-salesforce.blogspot.in/2013/05/sfdc-data-collections-listsetmap.html

 

raj_sfdccraj_sfdcc
Hi  Anilt,

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.
List Vs Set Vs Map InSalesforce (https://salessforcehacks.blogspot.com/2020/01/collections-in-salesforce-list-set-map.html)