© 1998-2002 McGraw-Hill

structure
Class ComparableAssociation

java.lang.Object
  |
  +--structure.Association
        |
        +--structure.ComparableAssociation
All Implemented Interfaces:
Comparable, Map.Entry

public class ComparableAssociation
extends Association
implements Comparable

A class implementing a comparable key-value pair. This class associates an immutable key with a mutable value. Useful for many other structures. Example usage:

To print out a list of professors sorted by the number of classes a particular student took from each, we could use the following:

 public static void main(String[] argv){
	//initialize a new fib heap
	FibHeap classesTaken = new FibHeap();

	//add professors and classes taken to a heap
	classesTaken.add(new ComparableAssociation(new Integer(5), "Andrea"));
	classesTaken.add(new ComparableAssociation(new Integer(1), "Barbara"));
	classesTaken.add(new ComparableAssociation(new Integer(3), "Bill"));
	classesTaken.add(new ComparableAssociation(new Integer(2), "Duane"));	
	classesTaken.add(new ComparableAssociation(new Integer(1), "Tom"));	

	//print out classes taken
	while(!classesTaken.isEmpty()){
	    ComparableAssociation p = (ComparableAssociation)classesTaken.remove();
	    System.out.println(p.getValue() + " is " + p.getKey() + " years old.");
	}
 }
 


Fields inherited from class structure.Association
theKey, theValue
 
Constructor Summary
ComparableAssociation(Comparable key)
          Construct an association that can be ordered, from only a key.
ComparableAssociation(Comparable key, Object value)
          Construct a key-value association that can be ordered.
 
Method Summary
 int compareTo(Object other)
          Determine the order of two comparable associations, based on key.
 String toString()
          Construct a string representation of the ComparableAssociation.
 
Methods inherited from class structure.Association
equals, getKey, getValue, hashCode, setValue
 
Methods inherited from class java.lang.Object
, clone, finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait
 

Constructor Detail

ComparableAssociation

public ComparableAssociation(Comparable key)
Construct an association that can be ordered, from only a key. The value is set to null.
Parameters:
key - The (comparable) key.

ComparableAssociation

public ComparableAssociation(Comparable key,
                             Object value)
Construct a key-value association that can be ordered.
Parameters:
key - The (comparable) key.
value - The (possibly comparable) associated value.
Method Detail

compareTo

public int compareTo(Object other)
Determine the order of two comparable associations, based on key.
Specified by:
compareTo in interface Comparable
Parameters:
other - The other comparable association.
Precondition:
other is non-null ComparableAssociation
Postcondition:
returns integer representing relation between values
Returns:
Value less-than equal to or greater than zero based on comparison

toString

public String toString()
Construct a string representation of the ComparableAssociation.
Overrides:
toString in class Association
Postcondition:
returns string representation
Returns:
The string representing the ComparableAssociation.

© 1998-2002 McGraw-Hill