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>();

Ex:

               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>();

Methods:                          

1. Add(<ElementName>):

Ex:

             countryNames.Add(‘India’);

            countryNames.Add(‘USA’);

           countryNames.Add(‘China’);

            countryNames.Add(‘Australia’);

             countryNames.Add(‘Japan’);

                              | Australia | China | India | Japan | USA |

2. AddAll(<CollectionName>):

      Ex:

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

                                             countryNames.AddAll(countries);

                              | 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():

Example:

// 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..

           countryNames.Add(‘India’);

           countryNames.Add(‘Bangladesh’);

          countryNames.Add(‘Australia’);

          countryNames.Add(‘China’);

          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’};   

         countryNames.AddAll(countries);

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

// Searching for a Element in the Collection..

         if(countryNames.Contains(‘Japan’))

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

        else

           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());

       }

      else

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

// Remove all Elements from the collection..

         countryNames.clear();

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