© 1998-2002 McGraw-Hill

structure
Class Table

java.lang.Object
  |
  +--structure.AbstractMap
        |
        +--structure.Table
All Implemented Interfaces:
Map, OrderedMap

public class Table
extends AbstractMap
implements OrderedMap

An implementation of an ordered dictionary. Key-value pairs are kept in the structure in order. To accomplish this, the keys of the table must be comparable.

Example Usage:

To create an alphebetized dictionary by reading a collection of words and definitions from System.in we could use the following:

 public static void main (String[] argv){
 	OrderedMap dict = new Table();
	ReadStream r = new ReadStream();
	String word, def;
	System.out.println("Enter a word: ");
	while(!r.eof()){
	    word = r.readLine();
	    System.out.println("Enter a definition: ");
	    def = r.readLine();
	    dict.put(word,def);
	    System.out.println("Enter a word: ");
	}
	System.out.println(dict);
 }
 

See Also:
Comparable

Field Summary
protected  OrderedStructure data
          An ordered structure that maintains the ComparableAssociations that store the key-value pairings.
 
Constructor Summary
Table()
          Construct a new, empty table.
Table(Table other)
           
 
Method Summary
 void clear()
          Remove all the elements of the table.
 boolean containsKey(Object key)
          Determine if the key is in the table.
 boolean containsValue(Object value)
          Returns true if the value is associated with some key in the table.
 Set entrySet()
          Return a structure containing all the entries in this Table
 Object get(Object key)
          Retrieve the value associated with the key provided.
 boolean isEmpty()
          Determine if the table is empty.
 Iterator iterator()
          Construct an iterator over the values of the table.
 Iterator keys()
          Construct an iterator over the keys of the table.
 Set keySet()
          Return a set containing the keys referenced by this data structure.
static void main(String[] argv)
           
 Object put(Object key, Object value)
          Enter a key-value pair into the table.
 Object remove(Object key)
          Remove a key-value pair, based on key.
 int size()
          Determine the number of key-value pairs within the table.
 String toString()
          Construct a string representing value of table.
 Structure values()
          Return a structure containing all the values referenced by this data structure.
 
Methods inherited from class structure.AbstractMap
hashCode, putAll
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait
 
Methods inherited from interface structure.Map
equals, hashCode, putAll
 

Field Detail

data

protected OrderedStructure data
An ordered structure that maintains the ComparableAssociations that store the key-value pairings.
Constructor Detail

Table

public Table()
Construct a new, empty table.

Table

public Table(Table other)
Method Detail

get

public Object get(Object key)
Retrieve the value associated with the key provided. Be aware, the value may be null.
Specified by:
get in interface Map
Parameters:
key - The key of the key-value pair sought.
Precondition:
key is a non-null object
Postcondition:
returns value associated with key, or null
Returns:
The value associated with the key.

put

public Object put(Object key,
                  Object value)
Enter a key-value pair into the table. if the key is already in the table, the old value is returned, and the old key-value pair is replaced. Otherwise null is returned. The user is cautioned that a null value returned may indicate there was no prior key-value pair, or --- if null values are inserted --- that the key was previously associated with a null value.
Specified by:
put in interface Map
Parameters:
key - The unique key in the table.
value - The (possibly null) value associated with key.
Precondition:
key is non-null object
Postcondition:
key-value pair is added to table
Returns:
The prior value, or null if no prior value found.

isEmpty

public boolean isEmpty()
Determine if the table is empty.
Specified by:
isEmpty in interface Map
Postcondition:
returns true iff table is empty
Returns:
True iff the table has no elements.

clear

public void clear()
Remove all the elements of the table.
Specified by:
clear in interface Map
Postcondition:
removes all elements from the table

keys

public Iterator keys()
Construct an iterator over the keys of the table. The order of the keys returned is in ascending order. It will be consistent with that of the iterator from elements, provided the table is not modified.
Postcondition:
returns an iterator for traversing keys of table
Returns:
An iterator over the keys of the table.

iterator

public Iterator iterator()
Construct an iterator over the values of the table. The order of the values returned is determined by order of keys. It will be consistent with that of the iterator returned from keys, provided the table is not modified.
Postcondition:
returns an iterator for traversing values in table
Returns:
An iterator over the values of the table.

containsKey

public boolean containsKey(Object key)
Determine if the key is in the table. The key should not be null.
Specified by:
containsKey in interface Map
Parameters:
key - A non-null key sought in the table.
Precondition:
key is non-null object
Postcondition:
returns true iff key indexes a value in table
Returns:
True iff the key is used in association with some value.

containsValue

public boolean containsValue(Object value)
Returns true if the value is associated with some key in the table. This is often difficult to implement efficiently.
Specified by:
containsValue in interface Map
Parameters:
value - The value sought (possibly null).
Precondition:
value is non-null object
Postcondition:
returns true iff value in table
Returns:
True, if the value is associated with some key in table.

remove

public Object remove(Object key)
Remove a key-value pair, based on key. The value is returned.
Specified by:
remove in interface Map
Parameters:
key - The key of the key-value pair to be removed.
Precondition:
key is non-null object
Postcondition:
removes value indexed in table
Returns:
The value associated with key, no longer in table.

size

public int size()
Determine the number of key-value pairs within the table.
Specified by:
size in interface Map
Postcondition:
returns number of key-value pairs in table
Returns:
The number of key-value pairs in the table.

keySet

public Set keySet()
Return a set containing the keys referenced by this data structure.
Specified by:
keySet in interface Map
Postcondition:
Returns a set containing the keys referenced by this data structure.
Returns:
a set containing the key referenced by this data structure.

values

public Structure values()
Return a structure containing all the values referenced by this data structure.
Specified by:
values in interface Map
Postcondition:
Returns a structure containing all the values referenced by this data structure.
Returns:
a structure containing all the values referenced by this data structure.

entrySet

public Set entrySet()
Return a structure containing all the entries in this Table
Specified by:
entrySet in interface Map
Postcondition:
Returns a structure containing all the entries in this Table
Returns:
a structure containing all the entries in this Table

toString

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

main

public static void main(String[] argv)

© 1998-2002 McGraw-Hill