Java.Util.Set is a collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.

Some set implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException. Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the set may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as "optional" in the specification for this interface.


Creating a Set, Insert, Retrieve and Remove

A set is a collection that holds unique values. Adding a value that's already in the set has no effect.

    // Create the set
    Set set = new HashSet();
   
    // Add elements to the set
    set.add("a");
    set.add("b");
    set.add("c");
   
    // Remove elements from the set
    set.remove("c");
   
    // Get number of elements in set
    int size = set.size();          // 2
   
    // Adding an element that already exists in the set has no effect
    set.add("a");
    size = set.size();              // 2
   
    // Determining if an element is in the set
    boolean b = set.contains("a");  // true
    b = set.contains("c");          // false
   
    // Iterating over the elements in the set
    Iterator it = set.iterator();
    while (it.hasNext()) {
        // Get element
        Object element = it.next();
    }
    
    // Create an array containing the elements in the set (in this case a String array)
    String[] array = (String[])set.toArray(new String[set.size()]);

 

Operating on Sets

    // Create the sets
    Set set1 = new HashSet();
    Set set2 = new HashSet();
   
    // Add elements to the sets ...
   
    // Copy all the elements from set2 to set1 (set1 += set2)
    // set1 becomes the union of set1 and set2
    set1.addAll(set2);
   
    // Remove all the elements in set1 from set2 (set1 -= set2)
    // set1 becomes the asymmetric difference of set1 and set2
    set1.removeAll(set2);
   
    // Get the intersection of set1 and set2
    // set1 becomes the intersection of set1 and set2
    set1.retainAll(set2);
   
    // Remove all elements from a set
    set1.clear();

Creating a Set That Retains Order-of-Insertion

    Set set = new LinkedHashSet();
   
    // Add some elements
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("2");
   
    // List the elements
    for (Iterator it=set.iterator(); it.hasNext(); ) {
        Object o = it.next();
    }
    // [1, 2, 3]