Sets

A set is an unordered collection of primitives that do not contain any duplicate elements. For example, the following table represents a set of city names:

'San Francisco' 'New York' 'Paris' 'Tokyo'
To declare a set, use the Set keyword followed by the primitive data type name within <> characters. For example:
new Set<String>()

The following are ways to declare and populate a set:

Set<String> s1 = new Set<String>{'a', 'b + c'}; // Defines a new set with two elements 
    
Set<String> s2 = new Set<String>(s1); // Defines a new set that contains the  
    
                                     // elements of the set created in the previous step 
    

To access elements in a set, use the system methods provided by Apex. For example:

Set<Integer> s = new Set<Integer>(); // Define a new set 
    
s.add(1);                            // Add an element to the set 
    
System.assert(s.contains(1));        // Assert that the set contains an element 
    
s.remove(1);                         // Remove the element from the set 
    

For more information, including a complete list of all supported set system methods, see Set Methods.

Note the following limitations on sets:
© Copyright 2000-2009 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.