How to Create Set Collection in Salesforce Apex? [With Various Methods]

Recently, I got a list of User IDs. In that list, there were a number of 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 the duplicate IDs, I created a set collection and used that to create a new list with unique IDs.

Here, I will explain how to create a set collection in Salesforce Apex programming and then see some methods for manipulating the set collection.

Set Collection in Salesforce Apex

Sets are similar to lists, but some properties of sets differ from those of lists. The elements that we store in the set collection are stored in an unordered format. The values we store in the set should be unique because they do not return duplicate values.

  • 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 restricting duplicate values in the Apex collection.

Syntax of declaring a Set collection in Apex:

Set<Datatype> mySet = new Set<Datatype>();

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 above code, you will see the set remove duplicate values and display only once. It also displays the set of values in ascending order.

How to Create Sets Collection in Salesforce Apex?

Methods For Set in Salesforce Apex Programming

We have seen what a set collection is and how to declare it. Now, we will see methods used in 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 is used to add new elements or values to the set.
  • Remove(): This method allows us to remove a value from the set collection.
  • 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() in the set collection is used to add a new element to the set collection. Using the add method, we can add individual values. In this method, when we add any element, it gets added at the last position, or we can say the element gets added in append.

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);
Salesforce Set Collection in Apex

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.

Here, first, I declared a set that stored string values. Using the remove() method, I wanted to remove the second indexed value and then 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.

Remove values from Set in Salesforce Apex

3. Size() Method in Set Collection

The Size() method in set collections is used in the Salesforce Apex program to check the number of elements present in the 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);
Size method in Set Collection in Salesforce Apex

4. Contain(value) Method in Set Collection

The contains() method is used to check if 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 some string values and want to check whether the Sharepoint value is present in the set collection. We use the contains() method and pass a value as a parameter for that. 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);
Contains Metho in Set Collection in Salesforce Apex

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 explained how to use the equal() method to check whether the values in a set, string, or integer are the same. I declare two sets, but don’t enter the Power BI value in the second set, so it will not be the same as the first one.

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);
Salesforce Apex Equal Method in Set Collection

These are the methods that we commonly use for set collection manipulation purposes using Salesforce Apex.

Conclusion

I hope you have got an idea about the set collection in Salesforce Apex and how the set is similar to the list collection in Apex. In this tutorial, I explained how to create a set collection in Salesforce Apex programming, and then I explained the syntax of some methods for manipulating the list collection with examples and their outputs.

You may like to read:


Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.