Today's Offer - Salesforce Certification Training - Enroll at Flat 10% Off.

- Salesforce Blogs -

How to Set Methods in Salesforce Apex?

What is set? A set is an unordered collection of elements that contain any type of data type like primitive types, collections, sObjects, user-defined types and built-in Apex types. Set do not allow duplicate .Insertion order is not preserved in the set. It also grows dynamically in run time like list. You cannot perform DML with Set. To declare a set, use the Set keyword followed by the primitive data type name within <> characters. For example

Set<account>Acc = new set<account> ();
Set<CustomObject__c>cust= new set<CustomObject __c>();

Set is collection type which contains multiple numbers of unordered unique records. A Set cannot have duplicate records Like Lists. salesforce quiz Set Methods

  • add(setElement)
  • addAll(fromList)
  • addAll(fromSet)
  • clear()
  • clone()
  • contains(setElement)
  • containsAll(listToCompare)
  • containsAll(setToCompare)
  • retainAll(listOfElementsToRetain)

Ø removeAll(setOfElementsToRemove)

  • isempty():

add(setElement): Adds an element to the set if it is not already present.

Read: What Apex Email in Salesforce?

set<string> names= new set<string>();

Here we have added ‘jack’ two times but set will not allow duplicate value, so debug output is Jack, Steve and Jorge. addAll(fromset) This method willaddallall value of the set to the other set.

Set<String> state = new Set<String>{'Alaska', 'Arizona ','California','Arkansas' };
set<String> s2 = new set<String>(state);

addAll(fromlist): Adds all of the elements in the specified list to the set if they are not already present.

list<String>mynamelist = new list<String>{'Alaska', 'Arizona ','California','Arkansas' };
set<String> name = new set<String>();
system.debug(name);// {Alaska, Arizona , Arkansas, California, Virginia}

Contains (Element); Returns true if the set contains the specified element.

Read: Salesforce Lightning Interview Questions and Answers

Set<String>myString = new Set<String>{'ABC', 'XYZ'};
Boolean result =myString.contains('ABC');
Boolean result1 = myString.contains('AXZ');

containsAll(setToCompare): Returns true if the set contains all of the elements in the specified set.

Set<String>myString = new Set<String>{'a', 'b'};
Set<String>sString = new Set<String>{'c'};
Set<String>rString = new Set<String>{'a', 'b', 'c'};

Boolean result1, result2;
result1 = myString.addAll(sString);
result2 = myString.containsAll(rString);
System.debug(result2);// True

retainAll(listOfElementsToRetain): This will keep  only the value that areexisting in list or set..

list<string>Names= new list<string>{'Dell','IBM','Apple'};
set<string>Orglist= new set<string>();
boolean result;


This method will removes the elements in the specified list from the set if they are present.

Read: How to Get Certified as a Salesforce Professional and Verify the Certification

set<integer>values= new set<integer>{10,20,30,40};
set<integer>ages= new set<integer>{10,20,30};

This method return true if the set has no element
set<integer>values= new set<integer>();
set<integer>values1= new set<integer>{10,20};
boolean flag=values.isempty();
boolean flag1=values1.isempty();
How to use set in example

Apex class
public class picklistNewExample {
public set<string>name         {set;get;}
public string selected          {set;get;}
public list<account>acclist     {set;get;}
public list<selectOption>option {set;get;}

option= new list<selectoption>();
selectOption op = new selectOption('none','-None-');
name= new set<string>();
list<account>ac=[select industry,name,type,phone from account where industry != null];
for(account a:ac){

for(string b:name){
SelectOption op1 = new selectOption(b,b);

public void DisplayRecord(){
acclist = new list<account>();
acclist = [select name,industry,phone,type from account where industry=:selected];

<apex:page controller="picklistNewExample" id="page" >
<apex:sectionHeader title="Account Record" description="PickList" help="help" />
<apex:form id="fm">
<apex:pageblock title="Choose PickList" tabStyle="Lead" id="pb" >
<apex:pageblocksection title="Select One" id="pbs">
<apex:selectList size="1" value="{!selected}">
<apex:selectOptions value="{!option}"/>
<apex:actionSupport event="onchange" action="{!displayRecord}"/>
<apex:pageBlockSection title="Selected Records" rendered="{! !IsNull(acclist)}">
<apex:pageBlockTable value="{!acclist}" var="a"  >
<apex:column value="{!}" />
<apex:column value="{!a.industry}" />
<apex:column value="{!a.type}" />

Preview How to Set Methods in Salesforce Apex

Salesforce Tutorial Overview

    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.

Trending Courses


  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

4 days 24 Nov 2019


  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

5 days 25 Nov 2019

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

5 days 25 Nov 2019


  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

6 days 26 Nov 2019


  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

-1 day 19 Nov 2019

Course for testing

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

34 days 24 Dec 2019


  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

13 days 03 Dec 2019

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

5 days 25 Nov 2019

SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

-1 day 19 Nov 2019


Search Posts


Receive Latest Materials and Offers on Salesforce Course