A set is a collection of unique, unordered elements. 

It can contain primitive datatypes (string, integer, date, etc.) , sObjects or user-defined objects.

The basic syntax of creating a set :

Set<datatype> set1 = new Set<datatype>;  OR

Set<datatype> = new Set<datatype>{value1, value2,…};

  1. The main characteristic of a set is the uniqueness of the elements. Yo can safely try to add the same element to a set more than once and it will disregard the duplicate without throwing an exception.

Set<String> collections = new Set<String>{‘Lists’, ’Sets’, ‘Maps’};


  1. However, there is a slight twist with sObject i.e. the

uniqueness of the data respects Case Sensitivity.

3. Uniqueness of sObject is determined by comparing fields in their object.

Our Recent Blog

Share This Post