This Java tip illustrates a method of creating a Set. A set is a group of unique elements. If an element is already present in a set it cannot be added in the set.

  // Create a set
    Set set = new HashSet();
    // Add some elements to the set
    // Remove elements from the set
    // Retrieve number of elements in the set
    int size = set.size();          // 2
    // Adding an element that already exists has no effect
    size = set.size();              // 2
    // Identifying the presence of an element 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 =;
    // Create an array (ex. string) of all elements in the set
    String[] array = (String[])set.toArray(new String[set.size()]);