Set Collection:

  • Set is an UnOrdered Collection, which allows us to store a group of elements of both “Homogeneous and Heterogeneous”.
  • Set Collection doesn’t preserves the insertion order. By default Set collection will arrange the elements in the Alphabetical Order.
  • Set Collection supports Dynamic Memory Allocation. Hence the collection size can Grow / Shrink at runtime.
  • Set Collection allows us to store the elements of Type “Primitive Type, SObject Type, Collection Type, Apex Type and User Defined Type”.
  • Set Collection maintains the Uniqueness of the elements by using “Binary Comparison”.
  • When the user tries to insert a duplicate element inside the collection, simply it will ignore the operation.

Syntax: Set<DataType> <objectName> = new Set<DataType>();


               Set<String> countryNames  = new Set<String>();

               Set<ID> recordIds = new Set<ID>();

               Set<Account> accountsSet = new Set<Account>();

               Set<Case> casesSet = new Set<Case>();

               Set<Loan__C> loansSet = new Set<Loan__C>();

               Set<Customer__C> customersSet = new Set<Customer__C>();


1. Add(<ElementName>):







                              | Australia | China | India | Japan | USA |

2. AddAll(<CollectionName>):


             String[] countries = new String[]{‘Bangladesh’,’Middle East’,’India’};


                              | Australia | Bangladesh| China | India | Japan | Middle East | USA |

               3. Integer Size():

               4. Boolean IsEmpty():

               5. Boolean Contains(<ElementName>):

               6. Remove(<ElementName>):     

               7. Boolean Equals(<SourceCollectionName>):

               8. Clone():

               9. Clear():


// Define a Set Collection to hold the elements..

           Set<String> countryNames = new Set<String>();

// Print the Collection Size..

           system.debug(‘Collection Size is…: ‘+ countryNames.size());

// Adding the Elements to collection..





          system.debug(‘After Adding Elements, Collection Size is…: ‘+ countryNames.size());

// Print the Collection elements..

         system.debug(‘Collection Elements are..: ‘+ countryNames);

// Adding Collection of Elements..

         string[] countries = new string[]{‘Middle East’,’Japan’,’india’,’West Indies’};   


         system.debug(‘Collection Elements..: ‘+ countryNames);

// Searching for a Element in the Collection..


          system.debug(‘Element Found in the Collection.’);


           system.debug(‘Element Not Found.’);

// Removing the Element from Collection.

             if(countryNames.Contains(‘Middle East’))


              countryNames.Remove(‘Middle East’);

              system.debug(‘After Removing the Element, Collection size is..: ‘+               coutryNames.size());



         system.debug(‘Element Not Found in the collection.’);

// Remove all Elements from the collection..


         system.debug(‘After Removing All, collection size is…: ‘+ countryNames.size());