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
cool 183cool 183 

Collections in salesforce

Amit Chaudhary 8Amit Chaudhary 8

1) List: List is an ordered collection of elements which will allow duplicates.
Syntax:
List<datatype> listName = new List<datatype>();
– The datatype allows both primitive datatypes and non-primitive datatypes
– Size of the list is dynamically increased.

Methods in List

Add: Add the values to the list.
Ex: List<String> colors =new List<String>();
colors.add(‘Red’);
colors.add(‘White’);
colors.add(‘Black’); (OR)
List<String> colors =new List<String>{‘Red’,’White’,’Black’}
Get: Retrieve a values from list using index.
String getcolor = colors.get(1); —- we get the value is ‘White’ in to getcolor veriable.
Set: Replaces a former value with the value at given index.
colors.set(1,’Green’); —- List has value at index ‘ 1 ‘ is White is changed to Green.
Size: Return the number of elements in the list.
colors.size(); —- Gives the size of colors list is ‘2’
Clear: Remove the elements from the list.
colors.clear();

Please check below link for more example
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm

2) Set is an unordered collection of elements which will not allow duplicates.

Syntax

set<datatype> setName = new set<datatype>();

data type allows only primitive datatypes and SObjects.
We cannot get the retrieve the data based on index because set does not  have index.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_sets.htm

3) Map

Map is key – value pairs.

Syntax

map<datatype,datatype> mapName = new map<datatype,datatype>();


Methods
Put()

Insert a key-value pair or replaces a value with the given value for the key .

   map.put(key,value);

Get()

Retrieves the value for a key.

map.get(key);

keySet()

Retrieves all the keys and return type is set;

map.keySet();

values()

Retrieves all the values and return type is list;

map.values();

Size()

Return the number of components in the map.

map.size();

Example: Create a map of Account Ids and Account objects.

public class AccountMapPopulation{

// Creating map with account id and account object

map <Id,Account> accountIdMap =new map<Id,Account>();

public void methodName(){

// creating the accounts

account acc1 = new account (name =’account1′ , industry = ‘ Banking’);

account acc2 =new account(name =’ account2′ , industry = ‘Agriculture’);

account acc3 = new account(name=’account3′ , industry=’Banking’);

// populating the map with account id and account object

accountIdMap .put(acc1.id,acc1);

accountIdMap .put(acc2.id,acc2);

accountIdMap .put(acc3.id,acc3);

}

}
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm

First datatype is key and it allows primitive datatypes and should be unique.
Second datatype is values and it allows both primitive & non-primitive datatypes and allows duplicates.

Please check one example in below post:-
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BLT2IAO


Please mark this as solution if this will help you :-
SonamSonam (Salesforce Developers) 
Did you get a chance to go through our help and training site:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections.htm

Please do as it have detailed info on the same with sample code usage.
ManojjenaManojjena
Hi dhivya ,

Collection in salesforce is not so vast as java .Apex has only 3 classes List,Set.Map.

Please check below links for details.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm    /FOR LIST 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_sets.htm   //FOR SET 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm   FOR MAP 

Below link tell you how to use map in VF page .

https://mindfiresfdcprofessionals.wordpress.com/2013/08/19/displaying-a-map-in-visual-force-page/   

Below link tell you how can we avoid limits using collection .

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BLhTIAW  


Let us know if any doubt you have .

Thanks 
Manoj
raj_sfdccraj_sfdcc
Hi,

Collections in Apex can be lists, sets, or maps.

Please find the below post for further exaplanation with helpful examples .

Salesforce Collections (https://salessforcehacks.blogspot.com/2020/01/collections-in-salesforce-list-set-map.html)