© 1998-2002 McGraw-Hill

structure
Class SetList

java.lang.Object
  |
  +--structure.AbstractStructure
        |
        +--structure.AbstractSet
              |
              +--structure.SetList
All Implemented Interfaces:
Set, Structure

public class SetList
extends AbstractSet

Implementation of a set of elements using a list as the underlying storage mechanism. As with the mathematical object, the elements of the set are not duplicated. No order is implied or enforced in this structure, but simple set operations such as intersection, union, difference, and subset are provided.

Example Usage: Given a list of students who completed a computer science thesis in the 2001-2002 academic year at Williams College and a list of graduating computer science majors who are continuing on to graduate school, we could determine which thesis students are planning to attend graduate school as follows:

 public static void main(String[] argv){
	//thesis students in the class of '02
	String[] thesis = new String[]{"Doug", "Evan", "Feng"};
	
	//students continuing on to grad school
	String[] grad = new String[]{"Doug", "Feng", "Lida"};

	//instantiate our sets
	Set thesisSet = new SetList(), 
	    gradSet = new SetList();
		
	//build sets up
	for(int i = 0; i < thesis.length; i++) thesisSet.add(thesis[i]);
	for(int i = 0; i < grad.length; i++) gradSet.add(grad[i]);
	
	//calculate the intersection of the two sets
	thesisSet.retainAll(gradSet);
	System.out.println(thesisSet);
 }
 


Field Summary
protected  List data
          The underlying structure --- a list.
 
Constructor Summary
SetList()
          Construct a new set list.
 
Method Summary
 void add(Object e)
          Add an element to set, if not already present.
 void addAll(Structure other)
          Compute the union of this set with other.
 void clear()
          Remove all the elements from the set.
 Object clone()
          Returns a shallow clone of this set.
 boolean contains(Object e)
          Returns true if value is an element of the set.
 boolean containsAll(Structure other)
          Determine if this set is a subset of other.
 boolean isEmpty()
          Determine if the set is empty.
 Iterator iterator()
          Construct an traversal to traverse the elements of the set.
 Object remove(Object e)
          Remove an element from the set.
 void removeAll(Structure other)
          Compute the difference between two sets.
 void retainAll(Structure other)
          Compute the intersection of this set and other.
 int size()
          Determine the number of elements in the set.
 String toString()
          Construct a string representation of the set.
 
Methods inherited from class structure.AbstractStructure
elements, hashCode, values
 
Methods inherited from class java.lang.Object
, equals, finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait
 
Methods inherited from interface structure.Structure
elements, values
 

Field Detail

data

protected List data
The underlying structure --- a list.
Constructor Detail

SetList

public SetList()
Construct a new set list.
Method Detail

clear

public void clear()
Remove all the elements from the set.
Postcondition:
elements of set are removed

isEmpty

public boolean isEmpty()
Determine if the set is empty.
Overrides:
isEmpty in class AbstractStructure
Postcondition:
returns true iff set is empty
Returns:
True iff there are no elements in set.

add

public void add(Object e)
Add an element to set, if not already present.
Parameters:
e - The new value to be added to set.
Precondition:
e is non-null object
Postcondition:
adds element e to interface

remove

public Object remove(Object e)
Remove an element from the set.
Parameters:
e - The element of the set to be removed.
Precondition:
e is non-null object
Postcondition:
e is removed from set, value returned
Returns:
The value actually removed.

contains

public boolean contains(Object e)
Returns true if value is an element of the set.
Overrides:
contains in class AbstractStructure
Parameters:
e - The element sought in set.
Precondition:
e is non-null
Postcondition:
returns true iff e is in set
Returns:
True iff the element is in the set.

containsAll

public boolean containsAll(Structure other)
Determine if this set is a subset of other.
Overrides:
containsAll in class AbstractSet
Parameters:
other - The potential superset.
Precondition:
other is non-null reference to set
Postcondition:
returns true iff this set is subset of other

clone

public Object clone()
Returns a shallow clone of this set.
Overrides:
clone in class Object
Postcondition:
returns a copy of set
Returns:
A new set with same values.

addAll

public void addAll(Structure other)
Compute the union of this set with other. This set not modified.
Overrides:
addAll in class AbstractSet
Parameters:
other - The set to be unioned with this.
Precondition:
other is non-null reference to set
Postcondition:
returns new set containing union of this and other
Returns:
Union of this and other.

retainAll

public void retainAll(Structure other)
Compute the intersection of this set and other. Members of result are in both this and other.
Overrides:
retainAll in class AbstractSet
Parameters:
other - The other set to be intersected with this.
Precondition:
other is non-null reference to set
Postcondition:
returns new set containing intersection of this and other
Returns:
Intersection of this and other.

removeAll

public void removeAll(Structure other)
Compute the difference between two sets. Values of the result are members of this, but not other.
Overrides:
removeAll in class AbstractSet
Parameters:
other - The set whose values are to be eliminated from this.
Precondition:
other is non-null reference to set
Postcondition:
returns new set containing difference of this and other
Returns:
Difference between this and other.

iterator

public Iterator iterator()
Construct an traversal to traverse the elements of the set. Elements will not appear in any particular order.
Postcondition:
returns traversal to traverse the elements of set
Returns:
An traversal for inspecting members of the set.

size

public int size()
Determine the number of elements in the set.
Postcondition:
returns number of elements in set
Returns:
The number of elements in the set.

toString

public String toString()
Construct a string representation of the set.
Overrides:
toString in class Object
Postcondition:
returns a string representation of set
Returns:
A string representing the set.

© 1998-2002 McGraw-Hill