· development · 3 min read

Salesforce Apex Set Class Guide

The Apex Set class provides various methods to work with sets like adding elements, removing elements, accessing elements and searching elements.

The Apex Set class provides various methods to work with sets like adding elements, removing elements, accessing elements and searching elements.

Introduction

In Salesforce Apex, a set is a collection that allows you to store unique elements of the same data type. A set is similar to a mathematical set, where each element is unique and there is no specific order maintained. In Salesforce Apex, the Set class provides methods and properties to work with sets efficiently.

Here are some key features and characteristics of Salesforce Apex sets:

  • Unique Elements: Sets in Apex only store unique elements. If you attempt to add a duplicate element to a set, it will be ignored.
  • Unordered Collection: Elements in a set do not have a specific order or index position. The order of elements is not guaranteed and can change.
  • Homogeneous Elements: A set in Apex can store elements of the same data type. For example, a set can hold a collection of strings, integers, custom objects, or any other data type.
  • Dynamic Size: Sets in Apex are dynamic, meaning they can grow or shrink as elements are added or removed. You don’t need to specify the initial size of the set.
  • Methods and Operations: The Apex Set class provides various methods and operations to manipulate and work with sets. These include adding elements, removing elements, checking for element presence and difference, and more.

Salesforce Apex sets are commonly used to store and manage collections of unique data, such as unique records or unique values from a larger dataset. They are particularly useful when you want to ensure uniqueness or perform set operations on a collection of data.

Apex set methods

Let’s take a look at some methods of the Set class.

add

To add an element to a set, use the add method.

Note: since sets only contain unique elements, add does nothing if the element is already in the set.

<Set>.add
Boolean add(Object setElement))
If the element is not already in the set, it will be added.
Input parameter
  • setElement (Object): The element that should be added to the set.
Return value
  • Boolean: Returns true, if the set was changed by adding an element. False, if the element was already in the set.
Example
Set<String> fruits = new Set<String>();
Boolean setUpdated = fruits.add('apple');
System.assertEquals(true,setUpdated);

addAll

To add a list of elements to a set, use the addAll method. All items from the list, that are not already in the set will be added.

<Set>.addAll
Boolean addAll(List<Object> fromList))
All elements from a given list get added to the set in case they are not already in the set.
Input parameter
  • fromList (List<Object>): A list of elements that should be added to this set.
Return value
  • Boolean: Returns true, if the set was changed by adding at least one element. False, if none of the elements was added to the set, because they were already in the set.
Example
Set<String> fruits = new Set<String>();
Boolean setUpdated = fruits.add('apple');

List<String> moreFruits = new List<String>();
moreFruits.add('banana');
moreFruits.add('orange');

fruits.addAll(moreFruits);

System.assertEquals(3,fruits.size());

List<String> evenMoreFruits = new List<String>();
evenMoreFruits.add('banana');

fruits.addAll(evenMoreFruits);
System.assertEquals(3,fruits.size());

The addAll method also takes other sets as input. All items of the added set that are not already in the original set will be added.

<Set>.addAll
Boolean addAll(Set<Object> fromSet))
All elements from a given set get added to the set in case they are not already in the set.
Input parameter
  • fromSet (Set<Object>): A set of elements that should be added to this set.
Return value
  • Boolean: Returns true, if the set was changed by adding at least one element. False, if none of the elements was added to the set, because they were already in the set.
Example
Set<String> fruits = new Set<String>();
Boolean setUpdated = fruits.add('apple');

Set<String> moreFruits = new Set<String>();
moreFruits.add('banana');
moreFruits.add('orange');

fruits.addAll(moreFruits);

System.assertEquals(3,fruits.size());

Set<String> evenMoreFruits = new Set<String>();
evenMoreFruits.add('banana');

fruits.addAll(evenMoreFruits);
System.assertEquals(3,fruits.size());

clear

To remove all elements of a set, use the clear method.

<Set>.clear
Void clear())
Clears the set by removing all of its elements.
Return value
  • Void: The clear method returns nothing.
Example
Set<String> fruits = new Set<String>();
fruits.add('apple');
System.assertEquals(1,fruits.size());
fruits.clear();
System.assertEquals(0,fruits.size());

contains

To find out, whether an element is in a set or not, use the contains method.

<Set>.contains
Boolean contains(Object setElement))
It returns true if the set includes the specified element.
Input parameter
  • setElement (Object): The element that might be within in the set.
Return value
  • Boolean: True, if the set includes the specified element.
Example
Set<String> fruits = new Set<String>();
fruits.add('apple');
System.assertEquals(1,fruits.size());
fruits.clear();
System.assertEquals(0,fruits.size());

containsAll

To verify that as list is a subset of a set you can use the containsAll method. ContainsAll returns true if all aof the elements in the list are in the set.

<Set>.containsAll
Boolean containsAll(List<Object> listToCompare))
Returns true if the set contains all the elements in the specified list. Both the set and the list must be of the same type.
Input parameter
  • listToCompare (List<Object>): List which elements might be in the set.
Return value
  • Boolean: True, if the set contains all the elements in the specified list.
Example
Set<String> fruits = new Set<String>();
fruits.add('apple');
fruits.add('banana');
fruits.add('orange');

List<String> moreFruits = new List<String>();
moreFruits.add('apple');
moreFruits.add('banana');

System.assertEquals(true,fruits.containsAll(moreFruits));
moreFruits.add('melon');
System.assertEquals(false,fruits.containsAll(moreFruits));

isEmpty

To check whether a set is empty or nut, use the isEmpty method.

<Set>.isEmpty
Boolean isEmpty())
Returns true if the set contains no elements.
Return value
  • Boolean: True, if the set contains no elements."
Example
Set<String> fruits = new Set<String>();
System.assertEquals(true,fruits.isEmpty());
fruits.add('apple');
System.assertEquals(false,fruits.isEmpty());

remove

To remove a single item from a set, use the remove method.

<Set>.remove
Boolean remove(Object setElement))
If the specified element is present, it will be removed from the set.
Input parameter
  • setElement (Object): The element that should be removed from the set.
Return value
  • Boolean: True, if the call resulted in a change to the original set.
Example
Set<String> fruits = new Set<String>();

fruits.add('apple');
System.assertEquals(true,fruits.contains('apple'));

fruits.remove('apple');
System.assertEquals(false,fruits.contains('apple'));

removeAll

To remove multiple items of a set in one call, use the removeAll method.

There two variants of the removeAll method. One takes a list as input and one a set.

This is removeAll with a list.

<Set>.removeAll
Boolean removeAll(List<Object> listOfElementsToRemove))
If any of the elements in the specified list are present, they will be removed from the set.
Input parameter
  • listOfElementsToRemove (List<Object>): The list of elements that should be removed from the set.
Return value
  • Boolean: True, if the set was changed by removing elements.
Example
List<String> moreFruits = new List<String>();
moreFruits.add('apple');
moreFruits.add('melon');
moreFruits.add('orange');

fruits.removeAll(moreFruits);

System.assertEquals(1,fruits.size());
System.assertEquals(true,fruits.contains('banana'));

This is removeAll with a set.

<Set>.removeAll
Boolean removeAll(Set<Object> setOfElementsToRemove))
If any of the elements in the specified set are present, they will be removed from the set.
Input parameter
  • setOfElementsToRemove (Set<Object>): The set of elements that should be removed from the set.
Return value
  • Boolean: True, if the set was changed by removing elements.
Example
Set<String> fruits = new Set<String>();
fruits.add('apple');
fruits.add('banana');
fruits.add('orange');

Set<String> moreFruits = new Set<String>();
moreFruits.add('apple');
moreFruits.add('melon');
moreFruits.add('orange');

fruits.removeAll(moreFruits);

System.assertEquals(1,fruits.size());
System.assertEquals(true,fruits.contains('banana'));

retainAll

You need to know the intersection between a set and a list or between two sets? Use the retainAll method.

RetainAll removes all elements that are not present in both sets or in the set and the list.

Here is the retainAll method with a list.

<Set>.retainAll
Boolean retainAll(List<Object> listOfElementsToRetain))
It keeps only the elements in this set that are also present in the specified list, removing any other elements.
Input parameter
  • listOfElementsToRetain (List<Object>): The list of elements to retain.
Return value
  • Boolean: If the method caused a change to the original set, it will return true.
Example
Set<String> fruits = new Set<String>();
fruits.add('apple');
fruits.add('banana');
fruits.add('orange');

List<String> moreFruits = new List<String>();
moreFruits.add('apple');
moreFruits.add('melon');
moreFruits.add('orange');

fruits.retainAll(moreFruits);

System.assertEquals(2,fruits.size());
System.assertEquals(true,fruits.contains('apple'));
System.assertEquals(true,fruits.contains('orange'));

Here is the retainAll method with a set.

<Set>.retainAll
Boolean retainAll(Set<Object> setOfElementsToRetain))
It keeps only the elements in this set that are also present in the specified list, removing any other elements.
Input parameter
  • setOfElementsToRetain (Set<Object>): The set of elements to retain.
Return value
  • Boolean: If the call resulted in a change to the original set, this method returns true.
Example
Set<String> fruits = new Set<String>();
fruits.add('apple');
fruits.add('banana');
fruits.add('orange');

Set<String> moreFruits = new Set<String>();
moreFruits.add('apple');
moreFruits.add('melon');
moreFruits.add('orange');

fruits.retainAll(moreFruits);

System.assertEquals(2,fruits.size());
System.assertEquals(true,fruits.contains('apple'));
System.assertEquals(true,fruits.contains('orange'));

size

To get the number of elements within a set, use the size method.

<Set>.size
Integer size())
Provides the count of elements in the set.
Return value
  • Integer: The count of elements in the set.
Example
Set<String> fruits = new Set<String>();
System.assertEquals(0,fruits.size());
fruits.add('apple');
System.assertEquals(1,fruits.size());
Share:
Back to Blog