In Salesforce Apex, collections play a very important role when working with multiple records or values. Instead of storing data in separate variables, collections allow us to manage data efficiently in a single structure.
One of the most useful collections in Apex is the Set, which is primarily used to store unique values.
This is very helpful in real-world scenarios such as removing duplicate records, handling bulk data, and optimizing performance.
Recently, I got a list of User IDs. In that list, there were several user IDs, some of which were duplicates. I need to remove those duplicate IDs and create a new list with only unique User IDs.
To remove duplicate IDs, I created a set and used it to create a new list with unique IDs.
Here, I will explain how to create a set collection in Salesforce Apex and then demonstrate some methods for manipulating it.
What is Set Collection in Apex?
Sets are similar to lists, but some properties of sets differ from those of lists. The elements in the set collection are stored in an unordered format. The values we store in the set should be unique, as it does not allow duplicates.
- When we execute the set, we get the values in ascending order as an output.
- If we enter duplicate values, the set removes them and displays only unique values, thereby preventing duplicates in the Apex collection.
Key Points of Set In Apex
- A Set stores unique values only
- Duplicate values are automatically removed
- It is unordered, meaning values do not maintain sequence
- It does not support indexing (like List)
- It can store primitive data types and objects
- It improves performance by avoiding duplicate processing
Syntax of Set in Apex
Set<Datatype> mySet = new Set<Datatype>();Set<String> technologies = new Set<String>();- String is the datatype
- technologies is the Set variable name
How to Create a Set Collection in Apex (Step-by-Step)
Below, I will explain several methods for declaring a set collection in Salesforce Apex.
Method 1: Create a Set with Values in Apex
In the Apex code below, I showed you how to declare a set collection in Salesforce Apex.
Set<Integer>SetInt= new Set<Integer>{4,6,3,2,7,2,8};
system.debug(' Values in Set are: ' +SetInt);
Set<String>SetString= new Set<String>{'Jira', 'Apex', 'MongoDB', 'VisualForce', 'LWC', 'Apex'};
system.debug(' Values in Set are: ' +SetString);As you execute the code above, you will see the set remove duplicate values and display them only once. It also displays the set of values in ascending order.

Method 2: Create an Empty Set and Add Values in Apex
Set<String> techSet = new Set<String>();
techSet.add('Apex');
techSet.add('LWC');
techSet.add('Flow');
techSet.add('Apex'); // Duplicate
System.debug(techSet);Output:
{Apex, LWC, Flow}Method 3: Convert Set to List in Apex
We can also convert the set collection to a list in Salesforce Apex.
Set<String> mySet = new Set<String>{'Apex','LWC'};
List<String> myList = new List<String>(mySet);Methods of Set in Salesforce Apex (Detailed Explanation)
We have seen what a set collection is and how to declare it. Now, we will look at the methods used for set manipulation in Salesforce Apex.
The following are the methods that we can use in the set, and I will explain all methods with examples:
- Add(): This method adds new elements or values to the set.
- Remove(): This method allows us to remove a value from a set.
- Size(): This is used to return the number of elements or values present in the set collection.
- Contain(): It is a Boolean method that returns true or false when we want to check if a value is present in the set.
- Equals(): It is used to compare two objects or variables to determine whether they are equal.
1. Add(Element) Method in Set Collection
The add() method in the set collection adds a new element to the set. Using the add method, we can add individual values. In this method, when we add an element, it is added at the end, or we can say it is appended.
Set<Integer>mySet = new Set<Integer>();
mySet.add (5);
mySet.add (3);
mySet.add (9);
mySet.add (2);
mySet.add (3);
system.debug( 'Values of Set Collection are: ' +mySet);
2. Remove(value) Method in Set Collection
The remove() method in a set collection deletes an element or value from a specific position in the set. This method manipulates the set collection by removing elements that are no longer needed.
First, I declared a set to store string values. Using the remove() method, I wanted to remove the second element and display the remaining values. In list collection, we provide the index value as a parameter to remove the element.
Set<String> TechSet = new Set<String>{'Salesforce', 'PowerBI', 'SharePoint', 'AWS'};
system.debug( ' Before removing values from Set: ' +TechSet);
TechSet.remove('PowerBI');
system.debug( ' After removing values from Set: ' +TechSet);As you execute the code, you can see the ‘Power BI’ string value has been removed from the set collection.

3. Size() Method in Set Collection
The Size() method in set collections is used in Salesforce Apex to determine the number of elements in a set collection. It is usually used to check the size of the collection before performing operations on the set.
Set<String> TechSet = new Set<String>{'Salesforce', 'PowerBI', 'SharePoint', 'AWS'};
Integer size = TechSet.size();
system.debug( ' The size of the TechSet is: ' +size);
4. Contain(value) Method in Set Collection
The contains() method checks whether a set contains a specific element. It returns true if the element is in the set and false if it’s not. This method is helpful when you want to know if an item is part of a collection.
Here, I have a set of string values and want to check whether a SharePoint value is present in the set. We use the contains() method and pass a value as a parameter. Then, declare a boolean variable to store the result.
Set<String> TechSet = new Set<String>{'Salesforce', 'PowerBI', 'SharePoint', 'AWS'};
Boolean b = TechSet.contains('SharePoint');
system.debug( ' Is SharePoint value present in collection Set: ' +b);
5. Equals() Method in Set Collection
Using the Equals() method, we can compare two objects or variables to determine whether they are equal. Basically, it is used to compare two different strings or integers.
In the Apex code below, I explain how to use the equal() method to check whether the values in a set, a string, or an integer are the same. I declare two sets, but don’t enter the Power BI value in the second set, so the second set won’t match the first.
As I execute this Apex code, let’s see the result.
Set<String> TechSet1 = new Set<String>{'Salesforce', 'PowerBI', 'SharePoint', 'AWS'};
Set<String> TechSet2 = new Set<String>{'Salesforce', 'SharePoint', 'AWS'};
Boolean SetEqual = TechSet1.equals(TechSet2);
system.debug( ' Is these Sets are equals: ' +SetEqual);
These are the methods we commonly use for set collection manipulation in Salesforce Apex.
Difference Between List and Set in Apex
| Feature | List | Set |
|---|---|---|
| Order | Maintains order | Unordered |
| Duplicate | Allowed | Not allowed |
| Index | Supported | Not supported |
| Performance | Slower (duplicate handling) | Faster |
| Use Case | Ordered data | Unique values |
When to Use Set in Apex?
A set should be used when you want to ensure that all values are unique. It is especially useful when working with record IDs and removing duplicates from data. This helps in avoiding unnecessary processing.
Another scenario where Set is useful is when you are working with bulk data. In triggers and batch jobs, handling duplicates efficiently is very important. A set helps in simplifying this process.
Using Set also improves performance by reducing the number of operations required to handle duplicates. This makes your code faster and more efficient. It is considered a best practice in many Salesforce development scenarios.
Conclusion
Set collection in Salesforce Apex is an essential tool for developers who want to write efficient and optimized code. It helps remove duplicates, improve performance, and handle bulk data effectively.
In real-world projects, using Set correctly can help avoid common issues such as duplicate processing and governor limit errors. If you are working with triggers, integrations, or large datasets, Set should always be part of your solution.
You may like to read:
- Variables and Data Types in Salesforce Apex
- Convert Integer Data Type to String in Salesforce Apex
- Create List Collection in Salesforce Apex?
- Create Set Collection in Salesforce Apex?
- Apex Triggers in Salesforce
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.