Blog

Salesforce Collection of Set

  • (4.0)
  • | 37 Ratings |
  • Last Updated February 04, 2019

Collection: When we want to save multiple data of “same type” and also connect with each other with some “common business rule”, then instead of creating multiple variables, we need to create one variable of type collection.

Same type = all the data are integers or all are strings.
Common business rule = For egage of all employees or salary of all employees.

Interested in mastering Salesforce? Enroll now for FREE demo on Salesforce Training

Collections are same like array” - We have 3 types of collections in Salesforce, and they are:

  1. List.
  2. Set.
  3. Map.

Set:

  • Sets are unordered collection of elements.
  • They will not contain any duplicate values.
  • Set elements can be primitive types (string,integer), SObject - (Salesforce Objects - Account, Lead, Cases etc), user defined types (classes).

Lets us look into collection-set with a sample program:

Scenario : Add salaries of employees in the list and process them with step by step execution with collection functions The below mentioned are 6 individual programs for adding salary of the employees via Set.

Checkout SalesForce Tutorial

Program#1 - How to create a “Set” to store Bank Acc Num of Employee?

Initial Code:

// declare a list variable
Set <integer> EmpBankAcc = New Set<integer> ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);
EmpBankAcc.add(444);

//values
system.debug(&#39;The Current Account Number = &#39; + EmpBankAcc);</integer></integer>

OutPut:

11:41:49:004 USER_DEBUG [11]|DEBUG|The Current Account Number = {111, 222, 333, 444}

Program#2 - How to find the size of the Acc Numbers created?

Initial Code:
// declare a list variable
Set <integer> EmpBankAcc = New Set<integer> ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);
EmpBankAcc.add(444);

//values
system.debug(&#39;The Current Account Numbers = &#39; + EmpBankAcc);

//for size
Integer length=EmpBankAcc.size();
system.debug(&#39; The Current size of the Bank Acc Numbers =  &#39; + length);</integer></integer>

OutPut:

12:05:35:002 USER_DEBUG [11]|DEBUG|The Current Account Numbers = {111, 222, 333, 444}
12:05:35:003 USER_DEBUG [15]|DEBUG| The Current size of the Bank Acc Numbers =  4

Program#3 - what if a duplicate value is added in “Set” in anonymous window?

Initial Code:
// declare a list variable
Set <integer> EmpBankAcc = New Set<integer> ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);

//values print
system.debug(&#39;The Current Account Numbers = &#39; + EmpBankAcc);

//duplicate value added
EmpBankAcc.add(111);

//duplicate  values print
system.debug(&#39;The Current Account Numbers = &#39; + EmpBankAcc);</integer></integer>

OutPut:

12:21:38:002 USER_DEBUG [11]|DEBUG|The Current Account Numbers = {111, 222, 333}
12:21:38:002 USER_DEBUG [15]|DEBUG|The Current Account Numbers = {111, 222, 333}

Note: It just ignore the duplicate values, added and displays the remaining values.

Checkout Salesforce Interview Questions

Program#4 - How to check whether the value (something assigned/add) is existing in the set or not?

Initial Code:
// declare a list variable
Set <integer> EmpBankAcc = New Set<integer> ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);

//values print
system.debug(&#39;The Current Account Numbers = &#39; + EmpBankAcc);

//check if set contains value
boolean CheckTheValue = EmpBankAcc.contains(444);

//print statement for checking the value existing or not?
System.debug ( &#39;Does this set contains 444? = &#39; + CheckTheValue);</integer></integer>

OutPut:

12:30:50:002 USER_DEBUG [10]|DEBUG|The Current Account Numbers = {111, 222, 333}
12:30:50:002 USER_DEBUG [14]|DEBUG|Does this set contains 444? = false

Program#5 - How to remove the value (something assigned/add) in the set?

Initial Code:
// declare a list variable
Set <integer> EmpBankAcc = New Set<integer> ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);

//values print
system.debug(&#39;The Current Account Numbers = &#39; + EmpBankAcc);

//Remove value from set
EmpBankAcc.remove(333);

//print statement for Current Set
system.debug(&#39;Current Set = &#39; + EmpBankAcc);</integer></integer>

OutPut:

12:39:02:003 USER_DEBUG [10]|DEBUG|The Current Account Numbers = {111, 222, 333}
12:39:02:003 USER_DEBUG [16]|DEBUG|Current Set = {111, 222}

Program#6 - How to check whether the value (something assigned/add) is empty in the set or not?

Initial Code:
// declare a list variable
Set <integer> EmpBankAcc = New Set<integer> ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);

//values print
system.debug(&#39;The Current Account Numbers = &#39; + EmpBankAcc);

//check if set contains value
boolean EmptyCheck = EmpBankAcc.isempty();

//print statement for checking the value existing or not?
System.debug ( &#39;Is this set empty? = &#39; + EmptyCheck);</integer></integer>

OutPut:

12:57:33:002 USER_DEBUG [10]|DEBUG|The Current Account Numbers = {111, 222, 333}
12:57:33:002 USER_DEBUG [16]|DEBUG|Is this set empty? = false

Assignment: Create set of 10 email IDs. Print one by one using 10 times using system.debug().
Hint: SET + for each loop.

In the next topic, we will discuss in detail about “Collection - Map”. Keep following us for more info on Salesforce Development / Programming.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

About Author

Arogyalokesh

Arogyalokesh takes complex technical ideas and distills them into user-friendly visuals to improve digital marketing campaigns for companies. He is a Content Writer at Mindmajix spreading nuggets of content related to Technology. Proceeded to work in a fast upscale environment, assuring client delight in content writings 


DMCA.com Protection Status