Set Class
Represents a collection of unique elements with no duplicate values.
The Set methods work on a set, that is, an unordered collection of elements that was initialized using the set keyword. Set elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types. 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.
- Uniqueness of set elements of user-defined types is determined by the
equalsandhashCodemethods, which you provide in your classes. Uniqueness of all other non-primitive types is determined by comparing the objects’ fields. - If the set contains String elements, the elements are case-sensitive. Two set elements that differ only by case are considered distinct.
For more information on sets, see Sets.
The following are constructors for Set.
-
Creates a new instance of the
Setclass. A set can hold elements of any data type T. -
Creates a new instance of the
Setclass by copying the elements of the specified set. T is the data type of the elements in both sets and can be any data type. -
Creates a new instance of the
Setclass by copying the list elements. T is the data type of the elements in the set and list and can be any data type.
Creates a new instance of the Set class. A set can hold elements of any data type T.
public Set<T>()
Creates a new instance of the Set class by copying the elements of the specified set. T is the data type of the elements in both sets and can be any data type.
public Set<T>(Set<T> setToCopy)
-
setToCopy:
Type: Set<T>
The set to initialize this set with.
Creates a new instance of the Set class by copying the list elements. T is the data type of the elements in the set and list and can be any data type.
public Set<T>(List<T> listToCopy)
-
listToCopy:
Type: Integer
The list to copy the elements of into this set.
The following are methods for Set. All are instance methods.
-
Adds an element to the set if it is not already present.
-
Adds all of the elements in the specified list to the set if they are not already present.
-
Adds all of the elements in the specified set to the set that calls the method if they are not already present.
-
Removes all of the elements from the set.
-
Makes a duplicate copy of the set.
-
Returns
trueif the set contains the specified element. -
Returns
trueif 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. -
Returns
trueif 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. -
Compares this set with the specified set and returns
trueif both sets are equal; otherwise, returnsfalse. -
Returns the hashcode corresponding to this set and its contents.
-
Returns
trueif the set has zero elements. -
Removes the specified element from the set if it is present.
-
removeAll(listOfElementsToRemove)
Removes the elements in the specified list from the set if they are present.
-
removeAll(setOfElementsToRemove)
Removes the elements in the specified set from the original set if they are present.
-
retainAll(listOfElementsToRetain)
Retains only the elements in this set that are contained in the specified list.
-
retainAll(setOfElementsToRetain)
Retains only the elements in the original set that are contained in the specified set.
-
Returns the number of elements in the set (its cardinality).
-
Returns the string representation of the set.
Adds an element to the set if it is not already present.
public Boolean add(Object setElement)
-
setElement:
Type: Object
Type: Boolean
This method returns true if the original set changed as a result of the call. For example:
Adds all of the elements in the specified list to the set if they are not already present.
public Boolean addAll(List<Object> fromList)
-
fromList:
Type: List
Type: Boolean
Returns true if the original set changed as a result of the call.
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.
Adds all of the elements in the specified set to the set that calls the method if they are not already present.
public Boolean addAll(Set<Object> fromSet)
-
fromSet:
Type: Set<Object>
Type: Boolean
This method returns true if the original set changed as a result of the call.
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.
Removes all of the elements from the set.
public Void clear()
Type: Void
Makes a duplicate copy of the set.
public Set<Object> clone()
Type: Set (of same type)
Returns true if the set contains the specified element.
public Boolean contains(Object setElement)
-
setElement:
Type: Object
Type: 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.
public Boolean containsAll(List<Object> listToCompare)
-
listToCompare:
Type: List<Object>
Type: 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.
public Boolean containsAll(Set<Object> setToCompare)
-
setToCompare:
Type: Set<Object>
Type: Boolean
Compares this set with the specified set and returns true if both sets are equal; otherwise, returns false.
public Boolean equals(Set<Object> set2)
-
set2:
Type: Set<Object>
The set2 argument is the set to compare this set with.
Type: Boolean
Two sets are equal if their elements are equal, regardless of their order. The == operator is used to compare the elements of the sets.
The == operator is equivalent to calling the equals method, so you can call set1.equals(set2); instead of set1 == set2;.
Returns the hashcode corresponding to this set and its contents.
public Integer hashCode()
Type: Integer
Returns true if the set has zero elements.
public Boolean isEmpty()
Type: Boolean
Removes the specified element from the set if it is present.
public Boolean remove(Object setElement)
-
setElement:
Type: Object
Type: Boolean
Returns true if the original set changed as a result of the call.
Removes the elements in the specified list from the set if they are present.
public Boolean removeAll(List<Object> listOfElementsToRemove)
-
listOfElementsToRemove:
Type: List<Object>
Type: Boolean
Returns true if the original set changed as a result of the call.
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.
Removes the elements in the specified set from the original set if they are present.
public Boolean removeAll(Set<Object> setOfElementsToRemove)
-
setOfElementsToRemove:
Type: Set<Object>
Type: Boolean
This method returns true if the original set changed as a result of the call.
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.
Retains only the elements in this set that are contained in the specified list.
public Boolean retainAll(List<Object> listOfElementsToRetain)
-
listOfElementsToRetain:
Type: List<Object>
Type: Boolean
This method returns true if the original set changed as a result of the call.
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.
Retains only the elements in the original set that are contained in the specified set.
public Boolean retainAll(Set setOfElementsToRetain)
-
setOfElementsToRetain:
Type: Set
Type: Boolean
Returns true if the original set changed as a result of the call.
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.
Returns the number of elements in the set (its cardinality).
public Integer size()
Type: Integer
Returns the string representation of the set.
public String toString()
Type: String
When used in cyclic references, the output is truncated to prevent infinite recursion. When used with large collections, the output is truncated to avoid exceeding total heap size and maximum CPU time.
- Up to 10 items per collection are included in the output, followed by an ellipsis (…).
- If the same object is included multiple times in a collection, it’s shown in the output only once; subsequent references are shown as
(already output).