Sub topic 5
Working with Set methods
This chapter assumes you know basic Apex syntax, have seen Lists, and can read a simple SOQL query. It covers the Set methods you will use most, and how to combine Sets.
Core methods
A Set holds unique values and has no index. You can't read an element by position. You add values, test for them, and remove them.
Set<String> tags = new Set<String>{'apex', 'soql'};
Boolean addedAgain = tags.add('apex'); // false, already present
Boolean addedNew = tags.add('trigger'); // true
System.debug(tags.size()); // 3
System.debug(tags.contains('soql')); // trueadd returns true when the set changed and false when the value was already there. That return value lets you detect duplicates while you build a set.
| Method | What it does |
|---|---|
| `add(e)` | Adds one element. Returns `true` if the set changed. |
| `addAll(collection)` | Adds every element of a List or Set. Returns `true` if the set changed. |
| `contains(e)` | Returns `true` if the element is present. |
| `containsAll(collection)` | Returns `true` if every element of the collection is present. |
| `remove(e)` | Removes one element. Returns `true` if it was present. |
| `removeAll(collection)` | Removes every listed element. |
| `retainAll(collection)` | Keeps only the elements that also appear in the collection. |
| `size()`, `isEmpty()`, `clear()` | Count the elements, test for empty, or empty the set. |
Removing duplicates from a List
Passing a List to the Set constructor is the quickest way to drop duplicates. Convert back to a List if you need ordering or indexing.
List<String> raw = new List<String>{'a', 'b', 'a', 'c', 'b'};
Set<String> unique = new Set<String>(raw);
System.debug(unique.size()); // 3
List<String> sorted = new List<String>(unique);
sorted.sort();
System.debug(sorted); // (a, b, c)Set algebra
addAll, retainAll, and removeAll change the set you call them on. They give you union, intersection, and difference.
Set<Integer> a = new Set<Integer>{1, 2, 3, 4};
Set<Integer> b = new Set<Integer>{3, 4, 5};
Set<Integer> union = a.clone();
union.addAll(b); // {1, 2, 3, 4, 5}
Set<Integer> intersection = a.clone();
intersection.retainAll(b); // {3, 4}
Set<Integer> difference = a.clone();
difference.removeAll(b); // {1, 2}
System.debug(a.containsAll(intersection)); // trueCommon errors
- Expecting an order. Don't depend on the order of iteration. Copy to a List and sort when order matters.
- Reading by index.
tags[0]andtags.get(0)don't compile for a Set. Usecontains, or iterate. - Case sensitivity. A Set of Strings compares values with case sensitivity, so
'Apex'and'apex'are two elements. This differs from the==operator, which ignores case for Strings. Lowercase values before adding them if you want them treated as the same. - Ignoring the return value.
addandremovereport whether anything changed. Use that instead of callingcontainsfirst.
Exercise
A List named emails contains duplicate addresses. Write code that produces a Set called unique of the addresses and prints how many addresses were duplicates.
Show answer
Set<String> unique = new Set<String>(emails); System.debug(emails.size() - unique.size()); The constructor drops duplicates, so the difference in size is the number of duplicate entries.
Exercise
Given Set<Id> a and Set<Id> b, how do you get the Ids that are in a but not in b without changing a?
Show answer
Set<Id> onlyInA = a.clone();
onlyInA.removeAll(b);
The clone protects a, and removeAll deletes every element that also appears in b.