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.
For more information on sets, see Sets.
The following are constructors for Set.
public Set<T>()
// Create a set of strings Set<String> s1 = new Set<String>(); // Add two strings to it s1.add('item1'); s1.add('item2');
public Set<T>(Set<T> setToCopy)
Set<String> s1 = new Set<String>(); s1.add('item1'); s1.add('item2'); Set<String> s2 = new Set<String>(s1); // The set elements in s2 are copied from s1 System.debug(s2);
public Set<T>(List<T> listToCopy)
List<Integer> ls = new List<Integer>(); ls.add(1); ls.add(2); // Create a set based on a list Set<Integer> s1 = new Set<Integer>(ls); // Elements are copied from the list to this set System.debug(s1);// DEBUG|{1, 2}
The following are methods for Set. All are instance methods.
public Boolean add(Object setElement)
Type: Boolean
Set<String> myString = new Set<String>{'a', 'b', 'c'}; Boolean result = myString.add('d'); System.assertEquals(true, result);
public Boolean addAll(List<Object> fromList)
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.
public Boolean addAll(Set<Object> fromSet)
Type: Boolean
This method returns true if the original set changed as a result of the call.
Set<String> myString = new Set<String>{'a', 'b'}; Set<String> sString = new Set<String>{'c'}; Boolean result1 = myString.addAll(sString); System.assertEquals(true, result1);
public Void clear()
Type: Void
public Set<Object> clone()
Type: Set (of same type)
public Boolean contains(Object setElement)
Type: Boolean
Set<String> myString = new Set<String>{'a', 'b'}; Boolean result = myString.contains('z'); System.assertEquals(false, result);
public Boolean containsAll(Set<Object> setToCompare)
Type: Boolean
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(true, result1); result2 = myString.containsAll(rString); System.assertEquals(true, result2);
public Boolean equals(Set<Object> set2)
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;.
public Integer hashCode()
Type: Integer
public Boolean isEmpty()
Type: Boolean
Set<Integer> mySet = new Set<Integer>(); Boolean result = mySet.isEmpty(); System.assertEquals(true, result);
public Boolean remove(Object setElement)
public Boolean removeAll(List<Object> listOfElementsToRemove)
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.
Set<integer> mySet = new Set<integer>{1, 2, 3}; List<integer> myList = new List<integer>{1, 3}; Boolean result = mySet.removeAll(myList); System.assertEquals(true, result); Integer result2 = mySet.size(); System.assertEquals(1, result2);
public Boolean removeAll(Set<Object> setOfElementsToRemove)
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.
public Boolean retainAll(List<Object> listOfElementsToRetain)
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.
Set<integer> mySet = new Set<integer>{1, 2, 3}; List<integer> myList = new List<integer>{1, 3}; Boolean result = mySet.retainAll(myList); System.assertEquals(true, result);
public Boolean retainAll(Set setOfElementsToRetain)
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.
public Integer size()
Type: Integer
Set<Integer> mySet = new Set<Integer>{1, 2, 3}; List<Integer> myList = new List<Integer>{1, 3}; Boolean result = mySet.retainAll(myList); System.assertEquals(true, result); Integer result2 = mySet.size(); System.assertEquals(2, result2);
public String toString()
Type: String