structure5
Class Table<K extends Comparable<K>,V>

java.lang.Object
  extended by structure5.AbstractMap<K,V>
      extended by structure5.Table<K,V>
All Implemented Interfaces:
Map<K,V>, OrderedMap<K,V>

public class Table<K extends Comparable<K>,V>
extends AbstractMap<K,V>
implements OrderedMap<K,V>

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

Constructor Summary
Table()
          Construct a new, empty table.
Table(Table<K,V> other)
           
 
Method Summary
 void clear()
          Remove all the elements of the table.
 boolean containsKey(K key)
          Determine if the key is in the table.
 boolean containsValue(V value)
          Returns true if the value is associated with some key in the table.
 Set<Association<K,V>> entrySet()
          Return a structure containing all the entries in this Table
 V get(K key)
          Retrieve the value associated with the key provided.
 boolean isEmpty()
          Determine if the table is empty.
 Iterator<V> iterator()
          Construct an iterator over the values of the table.
 Iterator<K> keys()
          Construct an iterator over the keys of the table.
 Set<K> keySet()
          Return a set containing the keys referenced by this data structure.
static void main(String[] argv)
           
 V put(K key, V value)
          Enter a key-value pair into the table.
 V remove(K 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<V> values()
          Return a structure containing all the values referenced by this data structure.
 
Methods inherited from class structure5.AbstractMap
hashCode, putAll
 
Methods inherited from class java.lang.Object
equals, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface structure5.Map
equals, hashCode, putAll
 

Constructor Detail

Table

public Table()
Construct a new, empty table.


Table

public Table(Table<K,V> other)
Method Detail

get

public V get(K key)
Retrieve the value associated with the key provided. Be aware, the value may be null.

Specified by:
get in interface Map<K extends Comparable<K>,V>
Parameters:
key - The key of the key-value pair sought.
Returns:
The value associated with the key.

put

public V put(K key,
             V 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<K extends Comparable<K>,V>
Parameters:
key - The unique key in the table.
value - The (possibly null) value associated with key.
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<K extends Comparable<K>,V>
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<K extends Comparable<K>,V>

keys

public Iterator<K> 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.

Returns:
An iterator over the keys of the table.

iterator

public Iterator<V> 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.

Returns:
An iterator over the values of the table.

containsKey

public boolean containsKey(K key)
Determine if the key is in the table. The key should not be null.

Specified by:
containsKey in interface Map<K extends Comparable<K>,V>
Parameters:
key - A non-null key sought in the table.
Returns:
True iff the key is used in association with some value.

containsValue

public boolean containsValue(V 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<K extends Comparable<K>,V>
Parameters:
value - The value sought (possibly null).
Returns:
True, if the value is associated with some key in table.

remove

public V remove(K key)
Remove a key-value pair, based on key. The value is returned.

Specified by:
remove in interface Map<K extends Comparable<K>,V>
Parameters:
key - The key of the key-value pair to be removed.
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<K extends Comparable<K>,V>
Returns:
The number of key-value pairs in the table.

keySet

public Set<K> keySet()
Return a set containing the keys referenced by this data structure.

Specified by:
keySet in interface Map<K extends Comparable<K>,V>
Returns:
a set containing the key referenced by this data structure.

values

public Structure<V> values()
Return a structure containing all the values referenced by this data structure.

Specified by:
values in interface Map<K extends Comparable<K>,V>
Returns:
a structure containing all the values referenced by this data structure.

entrySet

public Set<Association<K,V>> entrySet()
Return a structure containing all the entries in this Table

Specified by:
entrySet in interface Map<K extends Comparable<K>,V>
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
Returns:
String representing table.

main

public static void main(String[] argv)