Title: Set - SortedSet
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Collection - Set - SortedSet Interface

interface Set extends Collection

The methods in the Set inteface are the same as the Collection interface. We will do a little comparing of the Set interface with the List interface which are both derived from the Collection interface.

Set has the following properties:

1. A Set does not allow duplicates, while the List interface does allow duplicates.
2. A Set is not ordered, while a List is ordered.
3. A Set allows a null entry to be added. However only one null can be added (remember each element must be unique)
4. HashSet is the one class that Java provides that implements the Set interface.
5. You rerun the TryCollection.java program with the ArrayList changed to a HashSet class.
TryCollection.java
6. Because the methods are the same as collection, I have repeated them below.

interface Collection

Proto-Type | Description
boolean add(Object o) | Ensures that this collection contains the specified element (optional operation).
void clear() | Removes all of the elements from this collection (optional operation).
boolean contains(Object o) | Returns true if this collection contains the specified element.
boolean isEmpty() | Returns true if this collection contains no elements.
Iterator iterator() | Returns an iterator over the elements in this collection.
boolean remove(Object o) | Removes a single instance of the specified element from this collection, if it is present (optional operation). boolean retainAll(Collection c) Retains only the elements in this collection that are contained in the specified collection (optional operation).
int size() | Returns the number of elements in this collection.
Object[] toArray() | Returns an array containing all of the elements in this collection.
Object[] toArray(Object[] a) | Returns an array containing all of the elements in this collection whose runtime type is that of the specified array.

SortedSet Interface

1. This interface extends the Set interface, so it doesn't allow duplicates.
2. The major change between a SortedSet interface and a Set interface is that the SortedSet always keeps its elements sorted unlike the Set interface.
3. Because of the Sorted property, each element must be mutually comparable, which means that they have to be of the same type so they can be compared against one another. If you create your own class (remember the Dog class in the List notes), then be prepared to implement the Comparable interface in your class.
4. Because of the Sorted property, the interface has some methods that allow you to return subsections of the SortedSet.
5. Java provides the TreeSet class which implements SortedSet.

interface SortedSet extends Set

Proto-Type | Description
Object first() | Returns the first (lowest) element currently in this sorted set.
SortedSet headSet(Object toElement) | Returns a view of the portion of this sorted set whose elements are strictly less than toElement.
Object last() | Returns the last (highest) element currently in this sorted set.
SortedSet subSet(Object fromElement, Object toElement) | Returns a view of the portion of this sorted set whose elements range from fromElement, inclusive, to toElement, exclusive.
SortedSet tailSet(Object fromElement) | Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement.

The following program shows off these new interfaces. Remember, that you can load a list of names from:
names.txt

TrySortedSet.java


import tools.*;
import java.util.*;
class TrySortedSet
{
    SortedSet store = new TreeSet();
    void load(String file)
    {
        String str;
        MyInput in = new MyInput(file);
        do
        {
            str = in.get();
            if (str != null)
            {
                str = str.trim();
                store.add(str);
            }

        }while (str != null);
        // Execute toString method on TreeSet
        System.out.println(store);
    }
    void doCmd(MyInput input, String str)
    {
     if (str.equals("load"))
            {
                System.out.println("Enter filename to load");
                str = input.get();
                load(str);
            }
      else if (str.equals("first"))
            System.out.println("first = " +
                store.first());
      else if (str.equals("last"))
            System.out.println("last = " +
                store.last());
      else if (str.equals("headSet"))
            {
                System.out.println("Enter toElement");
                str = input.get();
                SortedSet s = store.headSet(str);
                System.out.println("headSet = " + s);
            }
      else if (str.equals("tailSet"))
            {
                System.out.println("Enter fromElement");
                str = input.get();
                SortedSet s = store.tailSet(str);
                System.out.println("tailSet = " + s);
            }

      else if (str.equals("subSet"))
            {
                System.out.println("Enter fromElement");
                str = input.get();
                System.out.println("Enter toElement");
                String str2 = input.get();
                SortedSet s = store.subSet(str, str2);
                System.out.println("subSet = " + s);
            }

      else if (str.equals("toString"))
                System.out.println(store);
      else if (str.equals("add"))
            {
                System.out.println("add what ?");
                str = input.get();
                System.out.println("adds(" + str + ") = " +
                    store.add(str));
            }
      else if (str.equals("remove"))
            {
                System.out.println("remove what ?");
                str = input.get();
                System.out.println("remove(" + str + ") = " +
                    store.remove(str));
            }
      else System.out.println("Illegal command");
    }
    public static void main(String[] args)
    {
        TrySortedSet doit = new TrySortedSet();
        String str;
        MyInput input = new MyInput();
        do
        {
            System.out.println(" **********************************");
            System.out.println("enter load, toString, add, remove  ");
            System.out.println("first, last, headSet, subSet, tailSet");
            System.out.println(" -----------------------");
            str = input.get();
            str = str.trim();

            doit.doCmd(input, str);
        } while (str != null);
    }
}




Create a simple class called Marriage which keeps track of husband/wife pairs. Using the Marriage class update some of the above java programs to store Collections of Marriages.

1. Update TrySortedSet.java

Create a new class derived from Set which provides Union and Intersection methods that work according to a Mathematicians ideas of Sets.

Last Updated: October 29, 2003 12:56 PM