The set methods work on a set, that is, an unordered collection of primitives or sObjects that was initialized using the set keyword. The set methods are all instance methods, that is, they all operate on a particular instance of a set. The following are the instance methods for sets.
| Name | Arguments | Return Type | Description |
|---|---|---|---|
| add | Set element e | Boolean | Adds an element to the set if it is not already present. This method returns true if the original set changed as a result
of the call. For example: set<string> myString = new Set<String>{'a', 'b', 'c'}; Boolean result; result = myString.add('d'); system.assertEquals(result, true); |
| addAll | List l | Boolean | Adds all of the elements in the specified list to the set if
they are not already present. This method results in the union of the list and the set. The list must be of the same type as the
set that calls the method. This method returns true if the original set changed as a result of the call. |
| addAll | Set s | Boolean | Adds all of the elements in the specified set to the set that
calls the method if they are not already present. This method results
in the union of the two sets. The specified set must be of
the same type as the original set that calls the method. This method
returns true if the original
set changed as a result of the call. For example: set<string> myString = new Set<String>{'a', 'b'}; set<string> sString = new Set<String>{'c'}; Boolean result1; result1 = myString.addAll(sString); system.assertEquals(result1, true); |
| clear | Void | Removes all of the elements from the set | |
| clone | Set (of same type) | Makes a duplicate copy of the set | |
| contains | Set element e | Boolean | Returns true if the
set contains the specified element. For example:set<string> myString = new Set<String>{'a', 'b'}; Boolean result; result = myString.contains('z'); system.assertEquals(result, false); |
| containsAll | List l | Boolean | Returns true if the set contains all of the elements in the specified list. The list must be of the same type as the set that calls the method. |
| containsAll | Set s | Boolean | Returns true if the
set contains all of the elements in the specified set. The specified
set must be of the same type as the original set that calls the method.
For example: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); system.assertEquals(result1, true); result2 = myString.containsAll(rString); system.assertEquals(result2, true); |
| isEmpty | Boolean | Returns true if the
set has zero elements. For example:Set<integer> mySet = new Set<integer>(); Boolean result; result = mySet.isEmpty(); system.assertEquals(result, true); |
|
| remove | Set Element e | Boolean | Removes the specified element from the set if it is present. This method returns true if the original set changed as a result of the call. |
| removeAll | List l | Boolean | Removes the elements in the specified list from the set if
they are present. This method results in the relative complement of the two sets. The list must be of the same type as the set that
calls the method. This method returns true if the original set changed as a result of the call.
For example: Set<integer> mySet = new Set<integer>{1, 2, 3}; List<integer> myList = new List<integer>{1, 3}; Boolean result = mySet.removeAll(myList); System.assertEquals(result, true); Integer result2 = mySet.size(); System.assertEquals(result2, 1); |
| removeAll | Set s | Boolean | Removes the elements in the specified set from the original
set if they are present. This method results in the relative
complement of the two sets. The specified set must be of the
same type as the original set that calls the method. This method returns true if the original set changed as a result of the call. |
| retainAll | List l | Boolean | Retains only the elements in this set that are contained in
the specified list. This method results in the intersection of the list and the set. The list must be of the same type as the
set that calls the method. This method returns true if the original set changed
as a result of the call. For example: Set<integer> mySet = new Set<integer>{1, 2, 3}; List<integer> myList = new List<integer>{1, 3}; Boolean result = mySet.retainAll(myList); System.assertEquals(result, true); |
| retainAll | Set s | Boolean | Retains only the elements in the original set that are contained
in the specified set. This method results in the intersection of the two sets. The specified set must be of the same type as the
original set that calls the method. This method returns true if the original set changed as a result of the call. |
| size | Integer | Returns the number of elements in the set (its cardinality).
For example:Set<integer> mySet = new Set<integer>{1, 2, 3}; List<integer> myList = new List<integer>{1, 3}; Boolean result = mySet.retainAll(myList); System.assertEquals(result, true); Integer result2 = mySet.size(); System.assertEquals(result2, 2); |
For more information on sets, see Sets.